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.
tunhienAsked:
Who is Participating?
 
pgloorCommented:
Why are you not simply using a timer to check every minute your time to remind?

Personally I never used a background worker, but to my understanding their purpose is more to to "one-time" tasks executed in parallel to the foreground task. A typical task I can think of is the loading of data in the background while while the first results are displayed in the foreground without delay. This can typically happens during startup or after the user invoked an action (usually by clicking a kind of button). If the data are loaded the background worker thread terminates.

Depending on your need a simple timer might not be good enough. In a similar situation I implemented an independent program that is loaded at system startup and looks up the users mailbox for new mail any 15 minutes. If new mail of a certain type is there the icon in the task bar changes and a beep sound indicates that the mailbox must be checked. This allows to check the mailbox for new mail even if the mail client is not loaded.

Peter
0
 
vo1dCommented:
i agree to pgloor. use a timer instead of a background worker for this.
0
 
topdog770Commented:
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 ).
0
 
vo1dCommented:
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?
0
 
topdog770Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.