Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a BackgroundWorker on a specific time?

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.

like image 813
G Basha Avatar asked Jun 09 '11 11:06

G Basha


People also ask

How do I start BackgroundWorker?

To use a BackgroundWorker, you simply tell it what time-consuming worker method to execute in the background, and then you call the RunWorkerAsync method. Your calling thread continues to run normally while the worker method runs asynchronously.

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

How does BackgroundWorker work in VB net?

BackgroundWorker enables a simple multithreaded architecture for VB.NET programs. This is more reliable, and easier, than trying to handle threads directly. In the Toolbox pane, please double-click on the BackgroundWorker icon. This will add a BackgroundWorker1 instance to the tray at the bottom of the screen.

What is BackgroundWorker in Winforms?

A BackgroundWorker component executes code in a separate dedicated secondary thread. In this article, I will demonstrate how to use the BackgroundWorker component to execute a time-consuming process while the main thread is still available to the user interface.


2 Answers

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

like image 136
Pranay Rana Avatar answered Oct 11 '22 04:10

Pranay Rana


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 ();
}
like image 33
Sam Holder Avatar answered Oct 11 '22 02:10

Sam Holder