終結執行緒Destroying threads

若要終止執行緒的執行,您通常會使用合作式取消模型To terminate the execution of the thread, you usually use the cooperative cancellation model. 有時候,無法以合作方式停止執行緒,因為它會執行協力廠商程式碼,而不是針對合作取消所設計。Sometimes it is not possible to stop a thread cooperatively, because it runs third-party code not designed for cooperative cancellation. .NET Framework 中的 Thread.Abort 方法可以用來強制終止 managed 執行緒。The Thread.Abort method in .NET Framework can be used to terminate a managed thread forcibly. 當您呼叫 Abort時,Common Language Runtime 會在目標執行緒中擲回一個 ThreadAbortException,以供目標執行緒攔截。When you call Abort, the Common Language Runtime throws a ThreadAbortException in the target thread, which the target thread can catch. 如需詳細資訊,請參閱Thread.AbortFor more information, see Thread.Abort. .NET Core 不支援 Thread.Abort 方法。The Thread.Abort method is not supported in .NET Core. 如果您需要在 .NET Core 中強制終止協力廠商程式碼的執行,請在個別的進程中執行,並使用 Process.KillIf you need to terminate the execution of third-party code forcibly in .NET Core, run it in the separate process and use Process.Kill.

注意

如果執行緒在其 Abort 方法被呼叫時正在執行非受控碼,執行階段就會將它標示為 ThreadState.AbortRequestedIf a thread is executing unmanaged code when its Abort method is called, the runtime marks it ThreadState.AbortRequested. 當執行緒返回受控碼時,會擲回例外狀況。The exception is thrown when the thread returns to managed code.

一旦中止執行緒之後,就無法重新啟動。Once a thread is aborted, it cannot be restarted.

Abort 方法不會造成執行緒立即中止,因為目標執行緒可以攔截 ThreadAbortException 並執行 finally 區塊中任意數量的程式碼。The Abort method does not cause the thread to abort immediately, because the target thread can catch the ThreadAbortException and execute arbitrary amounts of code in a finally block. 如果您需要等候,直到執行緒結束,則可呼叫 Thread.JoinYou can call Thread.Join if you need to wait until the thread has ended. Thread.Join 是一個封鎖呼叫,在執行緒實際停止執行或已經過選擇性的逾時間隔之後才會返回。Thread.Join is a blocking call that does not return until the thread has actually stopped executing or an optional timeout interval has elapsed. 中止的執行緒可以呼叫 ResetAbort 方法,或在 finally 區塊中執行未繫結的處理,因此,如果您未指定逾時,則不保證會等候結束。The aborted thread could call the ResetAbort method or perform unbounded processing in a finally block, so if you do not specify a timeout, the wait is not guaranteed to end.

正在對 Thread.Join 方法的呼叫中等候的執行緒可由呼叫 Thread.Interrupt 的其他執行緒來中斷。Threads that are waiting on a call to the Thread.Join method can be interrupted by other threads that call Thread.Interrupt.

處理 ThreadAbortExceptionHandling ThreadAbortException

如果您預期執行緒會中止,無論是從您自己的程式碼呼叫 Abort,還是卸載執行緒執行所在的應用程式定義域 (AppDomain.Unload 使用 Thread.Abort 來終止執行緒),您的執行緒都必須處理 ThreadAbortException 並執行 finally 子句中的任何最終處理,如下列程式碼所示。If you expect your thread to be aborted, either as a result of calling Abort from your own code or as a result of unloading an application domain in which the thread is running (AppDomain.Unload uses Thread.Abort to terminate threads), your thread must handle the ThreadAbortException and perform any final processing in a finally clause, as shown in the following code.

Try  
    ' Code that is executing when the thread is aborted.  
Catch ex As ThreadAbortException  
    ' Clean-up code can go here.  
    ' If there is no Finally clause, ThreadAbortException is  
    ' re-thrown by the system at the end of the Catch clause.   
Finally  
    ' Clean-up code can go here.  
End Try  
' Do not put clean-up code here, because the exception   
' is rethrown at the end of the Finally clause.  
try   
{  
    // Code that is executing when the thread is aborted.  
}   
catch (ThreadAbortException ex)   
{  
    // Clean-up code can go here.  
    // If there is no Finally clause, ThreadAbortException is  
    // re-thrown by the system at the end of the Catch clause.   
}  
// Do not put clean-up code here, because the exception   
// is rethrown at the end of the Finally clause.  

清除程式碼必須位於 catch 子句或 finally 子句,因為系統必須在 finally 子句結尾處重新擲回 ThreadAbortException,如果沒有 finally 子句,則會在 catch 子句結尾處重新擲回。Your clean-up code must be in the catch clause or the finally clause, because a ThreadAbortException is rethrown by the system at the end of the finally clause, or at the end of the catch clause if there is no finally clause.

您可以呼叫 Thread.ResetAbort 方法來防止系統重新擲回例外狀況。You can prevent the system from rethrowing the exception by calling the Thread.ResetAbort method. 但是,只有當您自己的程式碼會造成 ThreadAbortException,才應採取此作法。However, you should do this only if your own code caused the ThreadAbortException.

請參閱See also