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 方法進行呼叫以終結執行緒時,common language runtime 會擲回 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.

注意

當 managed 可執行檔中的所有前景執行緒都結束之後,common language runtime (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 使用 HRESULT COR_E_THREADABORTED,其值為0x80131530。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()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen 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)

適用於

另請參閱