ThreadAbortException ThreadAbortException ThreadAbortException ThreadAbortException Class

定义

当对 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
继承
ThreadAbortExceptionThreadAbortExceptionThreadAbortExceptionThreadAbortException
属性

示例

下面的示例演示如何中止线程。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 '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

此代码生成以下输出: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方法进行调用以销毁线程时, 公共语言运行时将ThreadAbortException引发。When 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.Abort它不会使用。When 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使用 COR_E_THREADABORTED 值为0x80131530 的 HRESULT。ThreadAbortException uses the HRESULT COR_E_THREADABORTED, which has the value 0x80131530.

备注

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

属性

Data Data Data Data

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

(Inherited from Exception)
ExceptionState ExceptionState ExceptionState ExceptionState

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

HelpLink HelpLink HelpLink HelpLink

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

(Inherited from Exception)
HResult HResult HResult HResult

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

(Inherited from Exception)
InnerException InnerException InnerException InnerException

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

(Inherited from Exception)
Message Message Message Message

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

(Inherited from Exception)
Source Source Source Source

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

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

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

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

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

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

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

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

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

(Inherited from Exception)
GetType() GetType() GetType() GetType()

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

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
ToString() ToString() ToString() ToString()

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

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

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

(Inherited from Exception)

适用于

另请参阅