如何立即取消正在工作的BackgroundWorker? [英] How to immediately cancel a working BackgroundWorker?

查看:218
本文介绍了如何立即取消正在工作的BackgroundWorker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BackgroundWorkerDoWork中花费时间.

每当我尝试通过backgroundWorker1.CancelAsync()取消作业时,backgroundWorker1.CancellationPending就会变为待处理",我应该等待DoWork中的下一次迭代以取消作业并自己退出.

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?

推荐答案

否.通常,您不能安全地立即"终止线程,因为该线程可能持有可能泄漏的资源,更重要的是,它可能持有锁.您需要组织工作人员遵守取消标志,并尽快安全地退出.

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.

这篇关于如何立即取消正在工作的BackgroundWorker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆