是否有可能杀掉的BackgroundWorker的主题? [英] Is it possible to kill the BackgroundWorker's thread?

查看:120
本文介绍了是否有可能杀掉的BackgroundWorker的主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能杀的线程的BackgroundWorker

在我的的DoWork 事件,我无法检查取消标志,因为我有一个阻挡 打电话到外部COM接口或查询到数据库。 CancelAsync 不取消调用COM。

In my DoWork event, I can't check the cancellation flag, because I have a blocking call to an external COM interface or a query to a database. CancelAsync doesn't cancel the call to COM.

我怎么能做到这一点吗?任何建议将是非常美联社preciated。

How can I do it, please ? Any suggestions will be very appreciated.

在此先感谢。

推荐答案

我不知道的安全的方式来终止线程​​。

I'm not aware of a safe way to abort a thread.

一个背景工人可以检查它是否应该取消及取消本身,而是它是否关闭查询数据库有没有什么可以做,直到它返回。

A background worker can check if it should cancel and cancel itself, but if it's off querying a database there's not much you can do until it returns.

您可以做的工作在一个线程池线程,只是放弃线程并启动另一个如果它不再需要(确保当它回来从数据库查询,它会检查是否应做什么讨厌的前取消)。当然,你需要平衡的性能和管理线程同步。如果你走这条路,你可以看看互锁静态类高效的关键部分锁定。

You can do work on a ThreadPool thread and just abandon the thread and start another if it's not needed anymore (making sure that when it comes back from a db query it checks if it should cancel before doing anything nasty). Of course you'd need to balance the performance and manage thread synchronisation. If you go down that path, you could take a look at the Interlocked static class for efficient critical section locking.

这篇关于是否有可能杀掉的BackgroundWorker的主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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