这有什么错用Thread.Abort的() [英] What's wrong with using Thread.Abort()

查看:97
本文介绍了这有什么错用Thread.Abort的()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道你不应该使用

So I know that you shouldn't use

Thread.Abort()

不过,我从来没有被赋予了很好的解释。有没有性能上的损失或一些隐藏的疑难杂症?

But I've never been given a good explanation. Is there a performance penalty or some hidden gotcha?

我知道你不能忽略/吞下ThreadAbortException(这是有道理的)

I know you can't ignore/swallow the ThreadAbortException (which makes sense)

推荐答案

在除了这里所有的其他很好的答案,让我补充一点,有没有保证,要Thread.Abort的通话将实际终止线程问题永远。这是可能的(虽然不是特别容易)对被中止变硬的线程。如果,例如,你是中止线程,因为你认为它运行敌对code,那么敌对code可以抵御自身的毁灭。

In addition to all of the other good answers here, let me add that there is no guarantee whatsoever that a call to Thread.Abort will actually abort the thread in question, ever. It is possible (though not particularly easy) to "harden" a thread against being aborted. If, for example, you are aborting a thread because you believe it to be running hostile code then the hostile code could be resisting its own destruction.

如果你有一个长期运行的操作,涉及code,你没有自己必须干净取下来,这样做的正确方法是放置code在其自身的过程的,而不是它自己的线程。 (和preferably在这一过程中的高度安全性限制应用程序域)。然后,您可以干净地终止进程。

If you have a long-running operation involving code that you do not own that must be taken down cleanly, the correct way to do this is to put that code in its own process, not its own thread. (And preferably in a highly security-restricted appdomain in that process.) You can then cleanly kill the process.

总之,Thread.Abort的是最好的指示设计不好,可能是不可靠的,极其危险的。它应该不惜一切代价避免;你曾经甚至考虑终止线程的唯一时间是在某种紧急停机code,你正试图拆掉一个AppDomain尽可能干净。

In short, Thread.Abort is at best indicative of bad design, possibly unreliable, and extremely dangerous. It should be avoided at all costs; the only time you should ever even consider aborting a thread is in some sort of "emergency shutdown" code where you are attempting to tear down an appdomain as cleanly as possible.

这篇关于这有什么错用Thread.Abort的()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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