ThreadAbortException 類別

定義

Abort(Object) 方法呼叫時所擲回的例外狀況。 此類別無法獲得繼承。

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

範例

下列範例示範如何中止執行緒。 接收的執行緒會 ThreadAbortException 使用 ResetAbort 方法來取消中止要求並繼續執行。

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

此程式碼會產生下列輸出:

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 會擲回 ThreadAbortExceptionThreadAbortException 是可攔截的特殊例外狀況,但會在區塊結尾自動引發 catch 。 當引發這個例外狀況時,執行時間會在 finally 結束執行緒之前執行所有區塊。 因為執行緒可以在區塊中進行未系結的計算 finally ,或呼叫 Thread.ResetAbort 以取消中止,所以不保證執行緒將會結束。 如果您想要等到中止的執行緒結束,您可以呼叫 Thread.Join 方法。 Join 是封鎖的呼叫,直到執行緒實際停止執行為止。

僅限 .Net Core: 雖然這種類型存在於 .NET Core 中,但因為 Abort 不受支援,所以 common language runtime 不會擲回 ThreadAbortException

注意

當 common language runtime (CLR) 在 managed 可執行檔中的所有前景執行緒都已結束之後停止背景執行緒,它就不會使用 Thread.Abort 。 因此,您無法使用 ThreadAbortException 來偵測 CLR 何時終止背景執行緒。

ThreadAbortException 使用 HRESULT COR_E_THREADABORTED,其值為0x80131530。

注意

繼承屬性的值 Data 一律為 null

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
ExceptionState

取得物件,其中包含有關執行緒中止之特定應用程式的資訊。

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已過時。

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱