How to start a BackgroundWorker on a specific time?

11,089

Solution 1

Run-time

Timer class represents a Timer control and used to create a Timer at run-time. The following code snippet creates a Timer at run-time, sets its property and event handler.

Timer t = new Timer();   
t.Interval = 2000;    
timer1.Enabled = true;  
timer1.Tick += new System.EventHandler(OnTimerEvent);

The event handler code looks like following.

private void OnTimerEvent(object sender, EventArgs e) 
{
     backgroundWorker1.RunWorkerAsync();
}

Here is demo : C# Timer Tutorial

Check documentation on msdn : http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

Solution 2

you could add a Timer setting the interval to how often you want the background worker to run and on the timers elapsed event you could start your background worker.

you will want to check that the background worker is not busy before you attempt to start it again though. If this situation occurs then you might consider immediately starting the background worker again when it completes. (if you want it to run at least once every 5 secs)

If you want it to wait 5 seconds after completion, then you need to stop the timer before you start the background worker, then in the background workers completed event you need to reset the timer and start it again.

EDIT

after one of your comments below it seems that you have many backgroundworkers, in which case using one of the other approaches which inserts a delay in the background workers completed event before starting the backgroundworker again is probably a better solution.

You could insert the delay using Thread.Sleep() as has been suggested or you could maybe create a timer in the function and assign a delegate to the timers elapsed event which restarted the background worker. Something along these (untested) lines:

private void backgroundWorker1_RunWorkerCompleted(object sender,     RunWorkerCompletedEventArgs e)
{
    Timer timer = new Timer();
    timer.Interval = 5000;
    timer.Enabled = true;
    timer.Elapsed+=delegate (object sender, ElapsedEventArgs args)
        {
        backgroundWorker1.RunWorkerAsync();        
        };
    timer.Start ();
}

Solution 3

Do you need it to run exactly every five seconds or not more often than five seconds? If it's the latter you could call Sleep(5000) on the Thread.CurrentThread just before your BackgroundWorker finishes its DoWork() method.

Solution 4

The easiest solution would be to let the thread sleep for 5 seconds in the beginning of backgroundWorker1_DoWork: Thead.Sleep(5000).

Alternatively, you can set a timer in RunWorkerCompleted that expires in 5 seconds and then starts the BackgroundWorker again.

Solution 5

If you want to use BW try this:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Thread.Sleep(5000);//wait for 5s then run it again
    backgroundWorker1.RunWorkerAsync();
}
Share:
11,089
G Basha
Author by

G Basha

Updated on June 04, 2022

Comments

  • G Basha
    G Basha almost 2 years

    I am having a problem in running a backgroundworker on a given specific time.

    My code runs the backgoundworker in only one second.

    I want to increase the Interval time in my background.

    I am using this line of code to run a background in a button click Event:

    private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }
    

    Then in backgroundWorker1_DoWork:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        backgroundWorker1.CancelAsync();
    }
    

    At last in backgroundWorker1_RunWorkCompleted:

    private void backgroundWorker1_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }
    

    I want to run the background continuously but for every 5 seconds or more than 5.

    There would be a great appreciation if someone could help me,

    Thanks in advance.

  • G Basha
    G Basha almost 13 years
    yes i can but its to long there are more than 20 backgroundworkers in it
  • G Basha
    G Basha almost 13 years
    your answer is good but i am unable to vote u because i have only 10 reputaions sorry
  • Yuck
    Yuck almost 13 years
    @G Basha - Thank you..I believe you can vote up at 15 reputation, unless you've used all your votes for the day.
  • G Basha
    G Basha almost 13 years
    your answer is good but i am unable to vote u because i have only 10 reputaions sorry
  • G Basha
    G Basha almost 13 years
    your answer is good but i am unable to vote u because i have only 10 reputaions sorry
  • G Basha
    G Basha almost 13 years
    your answer is good but i am unable to vote u because i have only 10 reputaions sorry
  • G Basha
    G Basha almost 13 years
    its hanging my application what i should
  • Renatas M.
    Renatas M. almost 13 years
    You welcome, you can do a favor by accepting answers to your other questions. You asked 8 questions and none of them had good answers?
  • Renatas M.
    Renatas M. almost 13 years
    Move Thread.Sleep(5000); before backgroundWorker1.CancelAsync(); in DoWork method to not hang your app as Yuck mentioned
  • Dbloom
    Dbloom almost 9 years
    I don't think System.Timers.Timer has a Tick event. Instead use Elapsed.