ThreadAbortException 類別

定義

當呼叫方法 Abort(Object) 時拋出的例外。 此類別無法獲得繼承。

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

範例

以下範例示範中止執行緒的做法。 接收執行 ThreadAbortException 緒會使用該 ResetAbort 方法取消中止請求並繼續執行。

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 該方法以銷毀執行緒時,通用語言執行時會拋 ThreadAbortException 出 .NET Framework 的 。 ThreadAbortException 是一個可以被捕捉的特殊例外,但在區 catch 塊結束時會自動再次被提出。 當此例外被提出時,執行時會執行所有 finally 區塊,然後結束執行緒。 由於執行緒可以在區 finally 塊中進行無界計算或呼叫 Thread.ResetAbort 取消中止,因此無法保證執行緒會終止。 如果你想等中止執行緒結束,可以呼叫該 Thread.Join 方法。 Join 是一個阻塞呼叫,直到執行緒實際停止執行才會回傳。

Note

僅限 .NET Core 與 .NET 5+: 即使這種類型存在於 .NET Core 和 .NET 5+,但由於 Abort 不支援,通用語言執行環境永遠不會拋出 ThreadAbortException

Note

當通用語言執行時(CLR)在受管理執行檔中所有前景執行緒結束後停止背景執行緒時,它不會使用 Thread.Abort。 因此,你無法用來 ThreadAbortException 偵測背景執行緒何時被 CLR 終止。

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

Note

繼承 Data 財產的價值總是 null

屬性

名稱 Description
Data

取得索引鍵/值組的集合,提供例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
ExceptionState

取得一個包含與執行緒中止相關的應用程式特定資訊的物件。

HelpLink

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

(繼承來源 Exception)
HResult

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

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

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

(繼承來源 Exception)
Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

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

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

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

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

在衍生類別中覆寫時,使用例外狀況的相關信息來設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前實例的運行時間類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Exception)

事件

名稱 Description
SerializeObjectState

發生於例外狀況串行化以建立例外狀況狀態物件,其中包含例外狀況的串行化數據。

(繼承來源 Exception)

適用於

另請參閱