C# 停止BackgroundWorker
我对后台工作人员有疑问。
我在后台工作人员中有无限循环。我怎样才能阻止它?
I have question about backgroundworker.
I have endless loop in backgroundworker. How can I stop it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改为非无限循环。
BackgroundWorker
内置了对取消的支持。要取消后台工作程序,请调用BackgroundWorker.CancelAsync< /代码>
。您还需要修改工作人员代码以检查取消,如文档中所述:
因此,例如,如果您的工作线程中有这个无限循环:
那么您可以将其更改为:
为了取消工作,您还需要设置属性
BackgroundWorker.WorkerSupportsCancellation
为true
。这可以在设计器中完成。Change it to a non-endless loop.
The
BackgroundWorker
has built-in support for cancellation. To cancel a background worker callBackgroundWorker.CancelAsync
. Also you need to modify the worker code to check for cancellation as mentioned in the documentation:So for example if you have this endless loop in your worker thread:
then you could change it to:
For cancellation to work you also need to set the property
BackgroundWorker.WorkerSupportsCancellation
totrue
. This can be done in the designer.