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
注釈
スレッドが作成されると、破棄されるまで 1 つ以上の ThreadState 状態になります。 スレッドがWaitSleepJoin状態のときにInterruptを呼び出すと、ターゲット スレッドでThreadInterruptedExceptionがスローされます。 スレッドが WaitSleepJoin 状態でない場合、スレッドがその状態になるまで例外はスローされません。 スレッドがブロックしない場合は、中断されることなく完了する可能性があります。
ThreadInterruptedException は、値が0x80131519を持つ HRESULT COR_E_THREADINTERRUPTEDを使用します。
ThreadInterruptedExceptionのインスタンスの初期プロパティ値の一覧については、ThreadInterruptedExceptionコンストラクターを参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| ThreadInterruptedException() |
既定のプロパティを使用して、 ThreadInterruptedException クラスの新しいインスタンスを初期化します。 |
| ThreadInterruptedException(SerializationInfo, StreamingContext) |
シリアル化されたデータを使用して、 ThreadInterruptedException クラスの新しいインスタンスを初期化します。 |
| ThreadInterruptedException(String, Exception) |
指定したエラー メッセージと、この例外の原因である内部例外への参照を使用して、 ThreadInterruptedException クラスの新しいインスタンスを初期化します。 |
| ThreadInterruptedException(String) |
指定したエラー メッセージを使用して、 ThreadInterruptedException クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| Data |
例外に関する追加のユーザー定義情報を提供するキーと値のペアのコレクションを取得します。 (継承元 Exception) |
| HelpLink |
この例外に関連付けられているヘルプ ファイルへのリンクを取得または設定します。 (継承元 Exception) |
| HResult |
特定の例外に割り当てられるコード化された数値である HRESULT を取得または設定します。 (継承元 Exception) |
| InnerException |
現在の例外の原因となった Exception インスタンスを取得します。 (継承元 Exception) |
| Message |
現在の例外を説明するメッセージを取得します。 (継承元 Exception) |
| Source |
エラーの原因となるアプリケーションまたはオブジェクトの名前を取得または設定します。 (継承元 Exception) |
| StackTrace |
呼び出し履歴のイミディエイト フレームの文字列表現を取得します。 (継承元 Exception) |
| TargetSite |
現在の例外をスローするメソッドを取得します。 (継承元 Exception) |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetBaseException() |
派生クラスでオーバーライドされた場合、1 つ以上の後続の例外の根本原因である Exception を返します。 (継承元 Exception) |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
派生クラスでオーバーライドされた場合は、例外に関する情報を使用して SerializationInfo を設定します。 (継承元 Exception) |
| GetType() |
現在のインスタンスのランタイム型を取得します。 (継承元 Exception) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
現在の例外の文字列形式を作成して返します。 (継承元 Exception) |
イベント
| 名前 | 説明 |
|---|---|
| SerializeObjectState |
例外に関するシリアル化されたデータを含む例外状態オブジェクトを作成するために例外がシリアル化されるときに発生します。 (継承元 Exception) |