共用方式為


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 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 是一個阻塞呼叫,直到執行緒實際停止執行才會回傳。

備註

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

備註

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

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

備註

繼承 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)

適用於

另請參閱