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

标签 c# multithreading abort

所以你看了标题,Thread.Abort()什么时候不会真正中止?我听过很多人说这是一个危险的命令,而且它永远不能保证工作,但我从来没有真正能够重现它。它一直对我有用..

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

最佳答案

你有没有读过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.

关于c# - Thread.Abort() 什么时候不会真正中止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5925628/

相关文章:

java - Java线程同步中的通知

c++ - 你能把 std::recursive_mutex 和 std::condition_variable 结合起来吗?

c# - 如何中止 .NET 任务?

c# - 数组作为属性。寻找更简单的方法来填充数组

c# - 检查有效日期

c# - C# 多线程应用程序可以为每个线程使用单独的 WorkingDirectories 吗?

database - 一笔交易中的 "commit"和 "abort"是什么意思?

c++ - 从 .txt 读取

c# - HTTPClient 缓冲区超过 2G;无法向缓冲区写入更多字节

c# - 从 Task.Run C# 中的 try/catch 返回异常