销毁线程

Abort 方法用于永久地停止托管线程。 调用 Abort 时,公共语言运行时在目标线程中引发 ThreadAbortException,目标线程可捕捉此异常。 有关更多信息,请参见 Thread.Abort

注意注意

如果调用线程的 Abort 方法时线程正在执行非托管代码,则运行时将其标记为 ThreadState.AbortRequested。线程返回到托管代码时引发此异常。

一旦线程被中止,它将无法重新启动。

Abort 方法不直接导致线程中止,因为目标线程可捕捉 ThreadAbortException 并在 finally 块中执行任意数量的代码。 如果需要等待线程结束,则可调用 Thread.JoinThread.Join 是一个阻塞调用,在线程实际停止执行之前或在可选的超时间隔结束之前,它不会返回。 中止的线程可调用 ResetAbort 方法,也可在 finally 块中执行无限处理,因此,如果不指定超时,则不能保证等待会结束。

等待对 Thread.Join 方法的调用的线程可由其他线程调用 Thread.Interrupt 来中断。

处理 ThreadAbortException

如果希望中止线程,可以从您的代码中调用 Abort,也可以卸载线程运行时所在的应用程序域(AppDomain.Unload 使用 Thread.Abort 终止线程),线程必须处理 ThreadAbortException 并在 finally 子句中执行所有最终处理,如下面的代码所示。

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 子句的结尾处再次引发该异常。

通过调用 Thread.ResetAbort 方法可以防止系统再次引发该异常。 但是,仅当您自己的代码引发 ThreadAbortException 时,才应这样做。

请参见

参考

ThreadAbortException

Thread

其他资源

使用线程和线程处理