Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
How to use Timer in Backgroundworker? Please give a example.In My Project , I used Timer in Backgroundworker. My Doubt is why doesn't Timer work in DOWORK Event?
Regards,
Lakshmi Narayanan.S
Posted
Updated 1-Aug-11 0:54am
v2

Using a timer in a background worker is kinda pointless. If you want to do something repeatedly, you should just use a loop iinside the backgroundworker DoWork() method. Here's some rough code:

C#
private void DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    int delay = 1000; // 1 second
    while (!worker.CancellationPending)
    {
        do something
        Thread.Sleep(delay);
    }
    e.Cancel = true;
}
If you need a delay longer than 1 second, your loop will have to change a little to allow you tpo detect whether or not the worker has been closed:

C#
int delay = 1000; // 1 second
int interval = 60000;
int elapsed = 0;
while (!worker.CancellationPending)
{
    elapsed = 0;
    do something
    while (elapsed < interval && !worker.CancellationPending)
    {
        Thread.Sleep(delay);
    }
}
e.Cancel = true;
 
Share this answer
 
v2
Comments
naraayanan 29-Jul-11 7:41am    
Thanks for you reply. If i create a Backgroundworker in the loop .How can i get the name of the Dowork Name.for example I create a 3 Backgroundworker in Loop. How can i get which backgroundworker is running ?please help me.
lukeer 29-Jul-11 7:59am    
Sounds as if your 3 BackgroundWorkers all execute the same DoWork() method?
However, John Simmons' solution covers that in BackgroundWorker worker = sender as BackgroundWorker;. The sender parameter tells you what BackgroundWorker started the method. You just need to change its data type to BackgroundWorker using cast or "as".
naraayanan 29-Jul-11 8:50am    
Thanks ..Please see my Code:
For example
no= 2.So I give 2 Backgroundworker
private void backworker_dowork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < no; i++)
{
if (backworker[i] == sender as BackgroundWorker)
{
}
}
In this code, How can i get the Sender Name .Please tell me.
Note: it is Windows Application.
lukeer 1-Aug-11 5:08am    
No need to iterate over an array of BackgroundWorkers. Just use
BackgroundWorker runningWorker = sender as BackgroundWorker;
or
BackgroundWorker runningWorker = (BackgroundWorker)sender;

Both variants give a reference to the currently running BackgroundWorker in runningWorker variable. Use it in whatever way you need to.
naraayanan 1-Aug-11 6:40am    
Hi friend,
Thanks...
The reason it doesn't work is that the WinForms timer runs in the UI thread, and background workers run in a background thread.

That said, you shouldn't be doing it. A background worker shouldn't need to wait for timed periods; if you have something that happens at timed intervals, you can have a timer in the main thread and start new work items when it fires. If you want to wait for input in a background worker, you can use a blocking I/O call. If it needs to wait for some other item to be complete, use EventWaitHandles (Manual- or AutoResetEvents).
 
Share this answer
 
v2
Comments
naraayanan 2-Aug-11 1:41am    
Thanks for your Reply.Can u give an Example.
Sergey Alexandrovich Kryukov 2-Aug-11 4:53am    
Not 100% accurate -- a timer itself can work in any thread unless this is a very bad System.Windows.Forms.Timer; but a timer callback of event handler might needs invocation inside. Howevder, it's really pointless. Very good idea for EventWaitHandle (will you fix a class name in your answer?) Overall, I voted 5.
--SA
BobJanova 2-Aug-11 12:54pm    
I did say 'WinForms timer' in my answer. It's the one that generally ends up being used because you already imported that namespace. Thanks for the correction.
naraayanan 2-Aug-11 6:47am    
Thanks SA,
can you give me an Example for this Task.It is very useful to my Project
naraayanan 16-Aug-11 5:00am    
Hi,
Where Can i start Timer in Background Worker?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900