ThreadAbortException 类

定义

当对 Abort(Object) 方法发出调用时引发的异常。The exception that is thrown when a call is made to the Abort(Object) method. 无法继承此类。This class cannot be inherited.

public ref class ThreadAbortException sealed : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class ThreadAbortException : SystemException
type ThreadAbortException = class
    inherit SystemException
Public NotInheritable Class ThreadAbortException
Inherits SystemException
继承
ThreadAbortException
属性

示例

下面的示例演示如何中止线程。The following example demonstrates aborting a thread. 接收 ThreadAbortException 的线程使用 ResetAbort 方法取消中止请求并继续执行。The thread that receives the ThreadAbortException uses the ResetAbort method to cancel the abort request and continue executing.

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." );
}

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."); 
    }
}
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
End Class


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
End Class

此代码生成以下输出:This code produces the following output:

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.  

注解

调用 Abort 方法来销毁线程时,公共语言运行时将引发 ThreadAbortExceptionWhen a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException 是一种特殊的异常,可被捕获,但会在 catch 块的末尾再次自动引发。ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. 引发此异常时,运行时会在结束线程之前执行所有 finally 块。When this exception is raised, the runtime executes all the finally blocks before ending the thread. 由于线程可在 finally 块中执行无限计算,或者调用 Thread.ResetAbort 以取消中止,因此不能保证线程将会终止。Because the thread can do an unbounded computation in the finally blocks or call Thread.ResetAbort to cancel the abort, there is no guarantee that the thread will ever end. 如果要等到中止的线程结束,可以调用 Thread.Join 方法。If you want to wait until the aborted thread has ended, you can call the Thread.Join method. Join 是一种阻止调用,直到线程实际停止执行时才返回。Join is a blocking call that does not return until the thread actually stops executing.

仅适用于 .Net Core: 尽管此类型存在于 .NET Core 中,但由于不支持 Abort,公共语言运行时将不会引发 ThreadAbortException.NET Core only: Even though this type exists in .NET Core, since Abort is not supported, the common language runtime won't ever throw ThreadAbortException.

备注

当公共语言运行时(CLR)在托管的可执行文件中的所有前台线程结束后停止后台线程时,它不会使用 Thread.AbortWhen the common language runtime (CLR) stops background threads after all foreground threads in a managed executable have ended, it does not use Thread.Abort. 因此,您不能使用 ThreadAbortException 来检测由 CLR 终止后台线程的时间。Therefore, you cannot use ThreadAbortException to detect when background threads are being terminated by the CLR.

ThreadAbortException 使用值为0x80131530 的 HRESULT COR_E_THREADABORTED。ThreadAbortException uses the HRESULT COR_E_THREADABORTED, which has the value 0x80131530.

备注

继承 Data 属性的值始终 nullThe value of the inherited Data property is always null.

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(继承自 Exception)
ExceptionState

获取一个对象,该对象包含与线程中止相关的应用程序特定的信息。Gets an object that contains application-specific information related to the thread abort.

HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(继承自 Exception)
Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(继承自 Exception)
TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(继承自 Exception)

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(继承自 Exception)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(继承自 Exception)
GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(继承自 Exception)

事件

SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅