backgroundworker:如何执行方法并支持取消_DoWork?
我参考了MSDN并发现通过触发事件来取消BackgroundWorker的_DoWork()非常容易。
我想知道如何在使用我在 _DoWork() 中调用的另一个方法中执行工作时取消 _DoWork()。
这可能吗?
谢谢,
马特
I have referred to MSDN and found out that it is quite easy to cancel a BackgroundWorker's _DoWork(), by trigger an event.
I would like to know how to cancel _DoWork() while performing work in another method that I've called within _DoWork().
Is this possible?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
BackgroundWorker
示例此处展示了如何做。总之:
BackgroundWorker
时,设置WorkerSupportsCancellation
。DoWork
处理程序中,定期检查CancellationPending
。如果为 true,则将DoWorkEventArgs.Cancel
设置为 true 并从DoWork
返回。CancelAsync
。RunWorkerCompletedEventArgs.Cancelled
。要从另一个方法中执行此操作,该方法必须将
BackgroundWorker
实例和DoWorkEventArgs
实例传递给它。The
BackgroundWorker
sample here shows how to do it.In summary:
BackgroundWorker
, setWorkerSupportsCancellation
.DoWork
handler, periodically checkCancellationPending
. If it is true, then setDoWorkEventArgs.Cancel
to true and return fromDoWork
.CancelAsync
.RunWorkerCompletedEventArgs.Cancelled
.To do this from within another method, that method must have the
BackgroundWorker
instance and theDoWorkEventArgs
instance passed to it.