Thread.Abort() 什么时候不会真正中止?

发布于 2024-11-05 09:40:36 字数 161 浏览 3 评论 0原文

所以你读了标题,Thread.Abort() 什么时候不会真正中止?我听很多人说这是一个危险的命令,并且永远不能保证它有效,但我实际上从未能够重现这一点。它总是对我有用。

是否有任何特定情况可能导致 Thread.Abort() 持续失败

So you read the title, when will Thread.Abort() not actually abort? I've heard lots of people say that its a dangerous command and that it is never guaranteed to work, but ive never actually been able to reproduce that. It's always worked for me..

Is there any specific situations that might cause Thread.Abort() to fail consistantly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-11-12 09:40:36

您是否阅读过文档

线程不保证立即中止,或者根本不中止。如果线程在作为中止过程的一部分调用的finally 块中执行无限量的计算,从而无限期地延迟中止,则可能会发生这种情况。要等到线程中止,您可以在调用 Abort 方法后调用线程上的 Join 方法,但不能保证等待会结束。

...

如果在尚未启动的线程上调用 Abort,则该线程将在调用 Start 时中止。如果在阻塞或休眠的线程上调用 Abort,则该线程将被中断,然后中止。

如果在已挂起的线程上调用 Abort,则会在调用 Abort 的线程中引发 ThreadStateException,并将 AbortRequested 添加到被中止线程的 ThreadState 属性中。在调用 Resume 之前,挂起的线程中不会抛出 ThreadAbortException。

如果在执行非托管代码时在托管线程上调用 Abort,则在线程返回到托管代码之前不会引发 ThreadAbortException。

如果同时进行两个 Abort 调用,则可能一个调用设置状态信息,另一个调用执行 Abort。但是,应用程序无法检测到这种情况。

在线程上调用Abort后,线程的状态包括AbortRequested。成功调用 Abort 导致线程终止后,线程的状态将更改为“已停止”。 如果有足够的权限,作为中止目标的线程可以使用 ResetAbort 方法取消中止。有关演示调用 ResetAbort 方法的示例,请参阅 ThreadAbortException 类。

Did you even read the documentation?

The thread is not guaranteed to abort immediately, or at all. This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. To wait until a thread has aborted, you can call the Join method on the thread after calling the Abort method, but there is no guarantee the wait will end.

...

If Abort is called on a thread that has not been started, the thread will abort when Start is called. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

If Abort is called on a thread that has been suspended, a ThreadStateException is thrown in the thread that called Abort, and AbortRequested is added to the ThreadState property of the thread being aborted. A ThreadAbortException is not thrown in the suspended thread until Resume is called.

If Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

If two calls to Abort come at the same time, it is possible for one call to set the state information and the other call to execute the Abort. However, an application cannot detect this situation.

After Abort is invoked on a thread, the state of the thread includes AbortRequested. After the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to Stopped. With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.

独行侠 2024-11-12 09:40:36

还有一种情况是Thread.Abort()调用没有失败,但是线程没有终止。抛出的ThreadAbortException可以被线程本身捕获。如果确实如此,然后在 catch 处理程序中调用 Thread.ResetAbort(),则不会在 catch 块末尾重新引发 ThreadAbortException。

There is also a situation where the Thread.Abort() call does not fail, but the thread does not terminate. The ThreadAbortException that is thrown can be caught by the thread itself. If it does and then calls Thread.ResetAbort() within the catch handler, the ThreadAbortException will not be rethrown at the end of the catch block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文