Link to home
Start Free TrialLog in
Avatar of tunhien
tunhien

asked on

BackgroundWorker in VS .Net 2005

Hi!
I'm programming a big program in C#

I want to write a small task in my program like this:

My user select a datetime that he wants to be reminded something. I will save his setting in a table in SQL Server 2000.

I want to create another thread running background whenever my program starts, detects and shows a message box to remind the user if it's the time to remind.

Can I use the control BackgroundWorker ?
I call the method "RunWorkerAsync" of BackgroundWorker in FormLoad event and "CancelAsync" in FormClosing.
and a bit of my source:
private void bckgrdWkrCaNhan_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            e.Result = Remind(worker, e);
        }
private void bckgrdWkrCaNhan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // First, handle the case where an exception was thrown.
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
            }
            else if (e.Cancelled)
            {
                MessageBox.Show("Cancel");
            }
            else
            {
                MessageBox.Show(e.Result.ToString());
               
            }
        }

private string Remind(BackgroundWorker worker, DoWorkEventArgs e)
        {
            string sResult= "";
            if (worker.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                //connect to DB and process something to make a result string (sResult)//will be showed in the message box to remind user
             
                            sResult = ....;
           
            }
            return sResult;
        }


BUT:
the code above just start when formload, implement immediately, not really what I want. I want it to run all the time so it can check the system clock every minute to remind user on time. How can I do this?
Please help me.
Thanks a lot.
Sorry for my English.
ASKER CERTIFIED SOLUTION
Avatar of pgloor
pgloor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vo1d
i agree to pgloor. use a timer instead of a background worker for this.
As further bit.. I tend to favor pgloor's recommendation of using a separate app for the reminder notification piece.
( it certainly can be controlled by the main program if desired ).
but implementing this functionality in an extra app make only sense, if that app has to run without the main app.
otherwise why going the hard way of dataexchange between both apps?
I'd agree with that too! :)

I guess I was thinking in terms of a small Notification Icon type app that would act as the 'alarm clock', etc.  ( just to minimize the foot print, etc, etc.. )  BUT even then, the main app could do everything and still be minimized to the notification tray