ThreadAbortException 类

在对 Abort 方法进行调用时引发的异常。无法继承此类。

**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ThreadAbortException
    Inherits SystemException
用法
Dim instance As ThreadAbortException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class ThreadAbortException : SystemException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class ThreadAbortException sealed : public SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ThreadAbortException extends SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public final class ThreadAbortException extends SystemException

备注

在调用 Abort 方法以销毁线程时,公共语言运行库将引发 ThreadAbortExceptionThreadAbortException 是一种可捕获的特殊异常,但在 catch 块的结尾处它将自动被再次引发。引发此异常时,运行库将在结束线程前执行所有 finally 块。由于线程可以在 finally 块中执行未绑定计算,或调用 Thread.ResetAbort 来取消中止,所以不能保证线程将完全结束。如果您希望一直等到被中止的线程结束,可以调用 Thread.Join 方法。Join 是一个模块化调用,它直到线程实际停止执行时才返回。

提示

在托管可执行文件中的所有前台线程已经结束后,当公共语言运行库 (CLR) 停止后台线程时,它不使用 System.Threading.Thread.Abort。因此,无法使用 ThreadAbortException 来检测 CLR 何时终止后台线程。

ThreadAbortException 使用值为 0x80131530 的 HRESULT COR_E_THREADABORTED。

提示

继承的 Data 属性的值始终是 空引用(在 Visual Basic 中为 Nothing)。

示例

下面的示例说明如何中止线程。接收 ThreadAbortException 的线程使用 ResetAbort 方法取消中止请求并继续执行。

Imports System
Imports System.Threading
Imports System.Security.Permissions


Public Class ThreadWork
   Public Shared Sub DoWork()
      Try
         Dim i As Integer
         For i = 0 To 99
            Console.WriteLine("Thread - working.")
            Thread.Sleep(100)
         Next i
      Catch e As ThreadAbortException
         Console.WriteLine("Thread - caught ThreadAbortException - resetting.")
         Console.WriteLine("Exception message: {0}", e.Message)
         Thread.ResetAbort()
      End Try
      Console.WriteLine("Thread - still alive and working.")
      Thread.Sleep(1000)
      Console.WriteLine("Thread - finished working.")
   End Sub 'DoWork
End Class 'ThreadWork


Class ThreadAbortTest
   Public Shared Sub Main()
      Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New Thread(myThreadDelegate)
      myThread.Start()
      Thread.Sleep(100)
      Console.WriteLine("Main - aborting my thread.")
      myThread.Abort()
      myThread.Join()
      Console.WriteLine("Main ending.")
   End Sub 'Main
End Class 'ThreadAbortTest
using System;
using System.Threading;
using System.Security.Permissions;

public class ThreadWork {
    public static void DoWork() {
        try {
            for(int i=0; i<100; i++) {
                Console.WriteLine("Thread - working."); 
                Thread.Sleep(100);
            }
        }
        catch(ThreadAbortException e) {
            Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
            Console.WriteLine("Exception message: {0}", e.Message);
            Thread.ResetAbort();
        }
        Console.WriteLine("Thread - still alive and working."); 
        Thread.Sleep(1000);
        Console.WriteLine("Thread - finished working.");
    }
}

class ThreadAbortTest {
    public static void Main() {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        Thread myThread = new Thread(myThreadDelegate);
        myThread.Start();
        Thread.Sleep(100);
        Console.WriteLine("Main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("Main ending."); 
    }
}
using namespace System;
using namespace System::Threading;
using namespace System::Security::Permissions;
ref class ThreadWork
{
public:
   static void DoWork()
   {
      try
      {
         for ( int i = 0; i < 100; i++ )
         {
            Console::WriteLine( "Thread - working." );
            Thread::Sleep( 100 );

         }
      }
      catch ( ThreadAbortException^ e ) 
      {
         Console::WriteLine( "Thread - caught ThreadAbortException - resetting." );
         Console::WriteLine( "Exception message: {0}", e->Message );
         Thread::ResetAbort();
      }

      Console::WriteLine( "Thread - still alive and working." );
      Thread::Sleep( 1000 );
      Console::WriteLine( "Thread - finished working." );
   }

};

int main()
{
   ThreadStart^ myThreadDelegate = gcnew ThreadStart( ThreadWork::DoWork );
   Thread^ myThread = gcnew Thread( myThreadDelegate );
   myThread->Start();
   Thread::Sleep( 100 );
   Console::WriteLine( "Main - aborting my thread." );
   myThread->Abort();
   myThread->Join();
   Console::WriteLine( "Main ending." );
}
import System.*;
import System.Threading.*;
import System.Security.Permissions.*;

public class ThreadWork
{
    public static void DoWork()
    {
        try {
            for (int i = 0; i < 100; i++) {
                Console.WriteLine("Thread - working.");
                System.Threading.Thread.Sleep(100);
            }
        }
        catch (ThreadAbortException e) {
            Console.WriteLine("Thread - caught ThreadAbortException"
                + " - resetting.");
            Console.WriteLine("Exception message: {0}", e.get_Message());
            System.Threading.Thread.ResetAbort();
        }
        Console.WriteLine("Thread - still alive and working.");
        System.Threading.Thread.Sleep(1000);
        Console.WriteLine("Thread - finished working.");
    } //DoWork
} //ThreadWork

class ThreadAbortTest
{
    public static void main(String[] args)
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        System.Threading.Thread myThread =
            new System.Threading.Thread(myThreadDelegate);
        myThread.Start();
        System.Threading.Thread.Sleep(100);
        Console.WriteLine("main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("main ending.");
    } //main
} //ThreadAbortTest

这段代码产生以下输出:

 Thread - working.
 Main - aborting my thread.
 Thread - caught ThreadAbortException - resetting.
 Exception message: Thread was being aborted.
 Thread - still alive and working.
 Thread - finished working.
 Main ending.

继承层次结构

System.Object
   System.Exception
     System.SystemException
      System.Threading.ThreadAbortException

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

ThreadAbortException 成员
System.Threading 命名空间
Thread 类
Thread.Abort

其他资源

销毁线程