在 BackGroundWorker 中使用计时器
我的应用程序处理实时数据。当打开 Form
时,会创建一个 BackgroundWorker
并获取数据并进行处理。
现在,只要 Form
处于活动状态或打开状态,我希望整个循环在 5 秒循环内运行。即,如果用户打开 form1
并在 5 秒内仍在其上,则 BackgroundWorker
将再次执行所有获取和处理操作。现在,如果用户关闭 form1
并打开 form2
,则会创建一个新的 BackgroundWorker
并执行与 form2
相关的处理>。
我已经完成了 BackgroundWorker
部分,但无法决定如何循环 BackgroundWorker
。我应该在 BackgroundWorker
中创建一个每 5 秒触发一次的计时器吗?或者我是否应该放弃 BackgroundWorker 并仅使用 Timer?
编辑:我在 Timer 中使用了 BGW。因此,计时器每 5 秒调用一次 BGW,如果 BGW 繁忙,则它会等待其完成。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,当然你可以使用计时器对象来做到这一点,如下所示
yeah of course you can do it using the timer object as shown below