Arcturus
Arcturus

Reputation: 437

Using Timer inside a BackGroundWorker

My application deals with real-time data. When a Form is opened, a BackgroundWorker is created and it fetches the data and does the processing.

Now I would like this entire cycle to run in a 5 second loop so long as the Form is active or open. ie if the user opens form1 and is still on it in 5 secs time then the BackgroundWorker will do all the fetching and processing again. Now if the user closes form1 and opens form2 then a new BackgroundWorker is created and it does the processing relevant to form2.

I'm done with the BackgroundWorker part but can't decide on how to loop the BackgroundWorker. Should I create a Timer inside the BackgroundWorker that fires every 5 seconds? Or do I chuck the BackgroundWorker and make do with just the Timer?

EDIT: I went with BGW inside Timer. So every 5 sec the timer calls BGW and if BGW is busy then it waits for it to complete.

Upvotes: 2

Views: 2304

Answers (1)

Ghyath Serhal
Ghyath Serhal

Reputation: 7632

yeah of course you can do it using the timer object as shown below

System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 5000;
timer.Start();

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    //do the logic..
}

Upvotes: 1

Related Questions