How to start BackgroundWorker at a specific time?

I had a problem starting a background artist at a given specific time.

My code runs backgoundworker in just one second.

I want to increase the time interval on my background.

I use this line of code to trigger the background by clicking the Event button:

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

Then in backgroundWorker1_DoWork :

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

Finally, in backgroundWorker1_RunWorkCompleted :

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

I want to constantly work with the background, but for every 5 seconds or more than 5.

It would be very nice if someone helped me,

Thanks in advance.

+6
source share
6 answers

at runtime

The Timer class is a Timer control and is used to create a timer at runtime. The following code fragment creates a timer at runtime, sets its property and event handler.

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

The code for the event handler is as follows.

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

Here is a demo: C # timer tutorial

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

+11
source

You can add a Timer interval value in how often you want the background worker to execute, and by the timer of a past event you could start your desktop.

You will want to verify that the background worker is not busy before trying to start it again. If such a situation arises, you can immediately start working with the background worker when it is completed. (if you want it to start at least once every 5 seconds)

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

EDIT

after one of your comments below, it seems like you have a lot of background workers, in which case using one of the other approaches that inserts a delay into the background worker populating the event before starting the background worker is probably the best solution.

You can insert a delay using Thread.Sleep() , as suggested, or you could create a timer in the function and assign a delegate to the timer-passed event that restarted the worker background. Something along these (unchecked) 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 (); } 
+5
source

The easiest solution is to let the sleep stream Thead.Sleep(5000) for 5 seconds at the beginning of backgroundWorker1_DoWork : Thead.Sleep(5000) .

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

+2
source

Do you need it to run exactly every five seconds, or no more than five seconds? If this is the last one, you can call Sleep(5000) on Thread.CurrentThread just before your BackgroundWorker finishes its DoWork() method.

+2
source

If you want to use BW, try the following:

 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Thread.Sleep(5000);//wait for 5s then run it again backgroundWorker1.RunWorkerAsync(); } 
+2
source

Could you use Timer to start BackgroundWorker ?

+1
source

Source: https://habr.com/ru/post/890134/


All Articles