开发者

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.


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..
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜