0

MSDN 说,对于Thread.Abort方法-

When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted.

So the exception would be thrown only once from the most immediate catch block, or from all encapsulating catch blocks ? Also, when saying unexecuted finally blocks are executed, does it include both totally unexecuted and partially executed blocks?

4

1 回答 1

2

如果块正在执行以及是否需要在堆栈回滚期间执行,则这些finally块始终受到完全保护。ThreadAbortException

是的,很明显,ThreadAbortException将在拦截它的同一线程中的任何块的末尾自动重新引发try... catch,否则它将毫无意义。ThreadAbortException只有两个“生命”有什么用?

这可以阻止 a 是否合乎逻辑ThreadAbortException

try
{
    try
    {
        // The Thread.Abort "happens" here
        Thread.Sleep(Timeout.Infinite);
    }
    catch
    {
    }
}
catch 
{
}

虽然这不会?

try
{
    // The Thread.Abort "happens" here
    Thread.Sleep(Timeout.Infinite);
}
catch
{
}
于 2013-08-20T08:43:23.467 回答