?
Solved

BackgroundWorker in VS .Net 2005

Posted on 2006-06-04
5
Medium Priority
?
272 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:tunhien
  • 2
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
pgloor earned 2000 total points
ID: 16829169
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16831664
i agree to pgloor. use a timer instead of a background worker for this.
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16833484
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
 
LVL 11

Expert Comment

by:vo1d
ID: 16843702
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
 
LVL 12

Expert Comment

by:topdog770
ID: 16846771
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question