ThreadStateException 類別

定義

當 a Thread 在 a 中 是 方法呼叫的無效 ThreadState 時拋出的例外。

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

範例

以下範例展示了一個錯誤,導致系統拋出一個 ThreadStateException

using System;
using System.Threading;

public class ThreadWork
{
   public static void DoWork()
   {
      Console.WriteLine("Working thread...");
   }
}

class ThreadStateTest
{
   public static void Main()
   {
      ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
      Thread myThread = new Thread(myThreadDelegate);
      myThread.Start();
      Thread.Sleep(0);
      Console.WriteLine("In main. Attempting to restart myThread.");
      try
      {
         myThread.Start();
      }
      catch (ThreadStateException e)
      {
         Console.WriteLine("Caught: {0}", e.Message);
      }
   }
}
Imports System.Threading

Public Class ThreadWork
   
   Public Shared Sub DoWork()
      Console.WriteLine("Working thread...")
   End Sub
End Class

Class ThreadStateTest
   
   Public Shared Sub Main()
      Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New Thread(myThreadDelegate)
      myThread.Start()
      Thread.Sleep(0)
      Console.WriteLine("In main. Attempting to restart myThread.")
      Try
         myThread.Start()
      Catch e As ThreadStateException
         Console.WriteLine("Caught: {0}", e.Message)
      End Try
   End Sub
End Class

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

In main. Attempting to restart myThread.
    Working thread...
    Caught: Thread is running or terminated. Cannot restart.

備註

一旦線程建立,它至少處於其中一個 ThreadState 狀態,直到終止。 ThreadStateException 由於執行緒目前狀態,無法執行請求操作的方法會拋出。 例如,嘗試透過呼叫 Start 已終止的執行緒來重新啟動一個中止的執行緒,會拋出一個 ThreadStateException

ThreadStateException 使用 HRESULT COR_E_THREADSTATE,其值為 0x80131520。

如需查看 ThreadStateException 實例的初始屬性值列表,請參閱 ThreadStateException 的建構子。

建構函式

名稱 Description
ThreadStateException()

初始化一個帶有預設屬性的新 ThreadStateException 類別實例。

ThreadStateException(SerializationInfo, StreamingContext)

使用串行化數據,初始化 ThreadStateException 類別的新實例。

ThreadStateException(String, Exception)

初始化類別的新實例 ThreadStateException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

ThreadStateException(String)

使用指定的錯誤訊息,初始化 ThreadStateException 類別的新實例。

屬性

名稱 Description
Data

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

(繼承來源 Exception)
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)

適用於

另請參閱