ThreadInterruptedException 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
例外是在等待 Thread 狀態下被中斷時拋出的。
public ref class ThreadInterruptedException : SystemException
[System.Serializable]
public class ThreadInterruptedException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ThreadInterruptedException : SystemException
public class ThreadInterruptedException : SystemException
[<System.Serializable>]
type ThreadInterruptedException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadInterruptedException = class
inherit SystemException
type ThreadInterruptedException = class
inherit SystemException
Public Class ThreadInterruptedException
Inherits SystemException
- 繼承
- 屬性
範例
以下程式碼範例展示了執行緒被中斷並隨後被阻塞時的行為。
using System;
using System.Security.Permissions;
using System.Threading;
class ThreadInterrupt
{
static void Main()
{
StayAwake stayAwake = new StayAwake();
Thread newThread =
new Thread(new ThreadStart(stayAwake.ThreadMethod));
newThread.Start();
// The following line causes an exception to be thrown
// in ThreadMethod if newThread is currently blocked
// or becomes blocked in the future.
newThread.Interrupt();
Console.WriteLine("Main thread calls Interrupt on newThread.");
// Tell newThread to go to sleep.
stayAwake.SleepSwitch = true;
// Wait for newThread to end.
newThread.Join();
}
}
class StayAwake
{
bool sleepSwitch = false;
public bool SleepSwitch
{
set{ sleepSwitch = value; }
}
public StayAwake(){}
public void ThreadMethod()
{
Console.WriteLine("newThread is executing ThreadMethod.");
while(!sleepSwitch)
{
// Use SpinWait instead of Sleep to demonstrate the
// effect of calling Interrupt on a running thread.
Thread.SpinWait(10000000);
}
try
{
Console.WriteLine("newThread going to sleep.");
// When newThread goes to sleep, it is immediately
// woken up by a ThreadInterruptedException.
Thread.Sleep(Timeout.Infinite);
}
catch(ThreadInterruptedException e)
{
Console.WriteLine("newThread cannot go to sleep - " +
"interrupted by main thread.");
}
}
}
Option Explicit
Option Strict
Imports System.Security.Permissions
Imports System.Threading
Public Class ThreadInterrupt
<MTAThread> _
Shared Sub Main()
Dim stayAwake As New StayAwake()
Dim newThread As New Thread(AddressOf stayAwake.ThreadMethod)
newThread.Start()
' The following line causes an exception to be thrown
' in ThreadMethod if newThread is currently blocked
' or becomes blocked in the future.
newThread.Interrupt()
Console.WriteLine("Main thread calls Interrupt on newThread.")
' Tell newThread to go to sleep.
stayAwake.SleepSwitch = True
' Wait for newThread to end.
newThread.Join()
End Sub
End Class
Public Class StayAwake
Dim sleepSwitchValue As Boolean = False
WriteOnly Property SleepSwitch As Boolean
Set
sleepSwitchValue = Value
End Set
End Property
Sub New()
End Sub
Sub ThreadMethod()
Console.WriteLine("newThread is executing ThreadMethod.")
While Not sleepSwitchValue
' Use SpinWait instead of Sleep to demonstrate the
' effect of calling Interrupt on a running thread.
Thread.SpinWait(10000000)
End While
Try
Console.WriteLine("newThread going to sleep.")
' When newThread goes to sleep, it is immediately
' woken up by a ThreadInterruptedException.
Thread.Sleep(Timeout.Infinite)
Catch ex As ThreadInterruptedException
Console.WriteLine("newThread cannot go to " & _
"sleep - interrupted by main thread.")
End Try
End Sub
End Class
備註
線程建立後,會處於一個或多個 ThreadState 狀態,直到被銷毀。 當執行緒處於狀態WaitSleepJoin時呼叫Interrupt,會導致 a ThreadInterruptedException 丟入目標執行緒。 如果執行緒不在該 WaitSleepJoin 狀態,例外不會拋出,直到執行緒進入該狀態。 如果線程永遠不會封鎖,它就可能完成,而不會中斷。
ThreadInterruptedException 使用 HRESULT COR_E_THREADINTERRUPTED,其值為 0x80131519。
如需查看 ThreadInterruptedException 實例的初始屬性值列表,請參閱 ThreadInterruptedException 的建構子。
建構函式
| 名稱 | Description |
|---|---|
| ThreadInterruptedException() |
初始化一個帶有預設屬性的新 ThreadInterruptedException 類別實例。 |
| ThreadInterruptedException(SerializationInfo, StreamingContext) |
使用串行化數據,初始化 ThreadInterruptedException 類別的新實例。 |
| ThreadInterruptedException(String, Exception) |
初始化類別的新實例 ThreadInterruptedException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。 |
| ThreadInterruptedException(String) |
使用指定的錯誤訊息,初始化 ThreadInterruptedException 類別的新實例。 |
屬性
| 名稱 | 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) |