How to immediately cancel a working BackgroundWorker?

核能气质少年 提交于 2019-12-07 04:01:08

问题


I have a time taking process in DoWork of my BackgroundWorker.

Whenever I try to Cancel the job by backgroundWorker1.CancelAsync(), the backgroundWorker1.CancellationPending becomes Pending and I should wait for the next iteration in my DoWork to Cancel the job and stepping out of it myself.

Is there any way to Cancel the job immediately after calling it?


回答1:


No. In general you can't safely terminate a thread "immediately", because it may be holding resources that would leak, and more importantly it may be holding locks. You need to structure your worker to respect the cancellation flag and and safely exit as soon as possible.



来源:https://stackoverflow.com/questions/9216314/how-to-immediately-cancel-a-working-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!