开发者

backgroundworker: How do I execute a method and support canceling _DoWork?

I have referred to MSDN and found out that it is quite easy to cancel a BackgroundWorker's _DoWork(), by trigger an e开发者_高级运维vent.

I would like to know how to cancel _DoWork() while performing work in another method that I've called within _DoWork().

Is this possible?

http://pastebin.com/wHrrBWKZ

Thanks,

Matt


The BackgroundWorker sample here shows how to do it.

In summary:

  • When you create the BackgroundWorker, set WorkerSupportsCancellation.
  • In your DoWork handler, periodically check CancellationPending. If it is true, then set DoWorkEventArgs.Cancel to true and return from DoWork.
  • To cancel the task, call CancelAsync.
  • To detect the task has been cancelled, observe RunWorkerCompletedEventArgs.Cancelled.

To do this from within another method, that method must have the BackgroundWorker instance and the DoWorkEventArgs instance passed to it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜