WaitHandle クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public MustInherit Class WaitHandle Inherits MarshalByRefObject Implements IDisposable
[ComVisibleAttribute(true)] public ref class WaitHandle abstract : public MarshalByRefObject, IDisposable

このクラスは、通常、同期オブジェクトの基本クラスとして使用されます。WaitHandle から派生したクラスは、共有リソースへのアクセスの取得または解放を示すシグナル通知機構を定義しますが、共有リソースへのアクセスを待機している間にブロックするには、継承した WaitHandle メソッドを使用します。

2 つのスレッドがバックグラウンド タスクを実行し、その間にメイン スレッドが WaitHandle クラスの WaitAny 静的メソッドおよび WaitAll 静的メソッドを使用してタスクの完了を待機できるようにするコード例を次に示します。
Imports System Imports System.Threading NotInheritable Public Class App ' Define an array with two AutoResetEvent WaitHandles. Private Shared waitHandles() As WaitHandle = _ {New AutoResetEvent(False), New AutoResetEvent(False)} ' Define a random number generator for testing. Private Shared r As New Random() <MTAThreadAttribute> _ Public Shared Sub Main() ' Queue two tasks on two different threads; ' wait until all tasks are completed. Dim dt As DateTime = DateTime.Now Console.WriteLine("Main thread is waiting for BOTH tasks to complete.") ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0)) ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1)) WaitHandle.WaitAll(waitHandles) ' The time shown below should match the longest task. Console.WriteLine("Both tasks are completed (time waited={0})", _ (DateTime.Now - dt).TotalMilliseconds) ' Queue up two tasks on two different threads; ' wait until any tasks are completed. dt = DateTime.Now Console.WriteLine() Console.WriteLine("The main thread is waiting for either task to complete.") ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0)) ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1)) Dim index As Integer = WaitHandle.WaitAny(waitHandles) ' The time shown below should match the shortest task. Console.WriteLine("Task {0} finished first (time waited={1}).", _ index + 1,(DateTime.Now - dt).TotalMilliseconds) End Sub 'Main Shared Sub DoTask(ByVal state As [Object]) Dim are As AutoResetEvent = CType(state, AutoResetEvent) Dim time As Integer = 1000 * r.Next(2, 10) Console.WriteLine("Performing a task for {0} milliseconds.", time) Thread.Sleep(time) are.Set() End Sub 'DoTask End Class 'App ' This code produces output similar to the following: ' ' Main thread is waiting for BOTH tasks to complete. ' Performing a task for 7000 milliseconds. ' Performing a task for 4000 milliseconds. ' Both tasks are completed (time waited=7064.8052) ' ' The main thread is waiting for either task to complete. ' Performing a task for 2000 milliseconds. ' Performing a task for 2000 milliseconds. ' Task 1 finished first (time waited=2000.6528).
using System; using System.Threading; public sealed class App { // Define an array with two AutoResetEvent WaitHandles. static WaitHandle[] waitHandles = new WaitHandle[] { new AutoResetEvent(false), new AutoResetEvent(false) }; // Define a random number generator for testing. static Random r = new Random(); static void Main() { // Queue up two tasks on two different threads; // wait until all tasks are completed. DateTime dt = DateTime.Now; Console.WriteLine("Main thread is waiting for BOTH tasks to complete."); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]); WaitHandle.WaitAll(waitHandles); // The time shown below should match the longest task. Console.WriteLine("Both tasks are completed (time waited={0})", (DateTime.Now - dt).TotalMilliseconds); // Queue up two tasks on two different threads; // wait until any tasks are completed. dt = DateTime.Now; Console.WriteLine(); Console.WriteLine("The main thread is waiting for either task to complete."); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]); ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]); int index = WaitHandle.WaitAny(waitHandles); // The time shown below should match the shortest task. Console.WriteLine("Task {0} finished first (time waited={1})." , index + 1, (DateTime.Now - dt).TotalMilliseconds); } static void DoTask(Object state) { AutoResetEvent are = (AutoResetEvent) state; int time = 1000 * r.Next(2, 10); Console.WriteLine("Performing a task for {0} milliseconds.", time); Thread.Sleep(time); are.Set(); } } // This code produces output similar to the following: // // Main thread is waiting for BOTH tasks to complete. // Performing a task for 7000 milliseconds. // Performing a task for 4000 milliseconds. // Both tasks are completed (time waited=7064.8052) // // The main thread is waiting for either task to complete. // Performing a task for 2000 milliseconds. // Performing a task for 2000 milliseconds. // Task 1 finished first (time waited=2000.6528).
using namespace System; using namespace System::Threading; public ref class WaitHandleExample { // Define a random number generator for testing. private: static Random^ random = gcnew Random(); public: static void DoTask(Object^ state) { AutoResetEvent^ autoReset = (AutoResetEvent^) state; int time = 1000 * random->Next(2, 10); Console::WriteLine("Performing a task for {0} milliseconds.", time); Thread::Sleep(time); autoReset->Set(); } }; int main() { // Define an array with two AutoResetEvent WaitHandles. array<WaitHandle^>^ handles = gcnew array<WaitHandle^> { gcnew AutoResetEvent(false), gcnew AutoResetEvent(false)}; // Queue up two tasks on two different threads; // wait until all tasks are completed. DateTime timeInstance = DateTime::Now; Console::WriteLine("Main thread is waiting for BOTH tasks to " + "complete."); ThreadPool::QueueUserWorkItem( gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]); ThreadPool::QueueUserWorkItem( gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]); WaitHandle::WaitAll(handles); // The time shown below should match the longest task. Console::WriteLine("Both tasks are completed (time waited={0})", (DateTime::Now - timeInstance).TotalMilliseconds); // Queue up two tasks on two different threads; // wait until any tasks are completed. timeInstance = DateTime::Now; Console::WriteLine(); Console::WriteLine("The main thread is waiting for either task to " + "complete."); ThreadPool::QueueUserWorkItem( gcnew WaitCallback(WaitHandleExample::DoTask), handles[0]); ThreadPool::QueueUserWorkItem( gcnew WaitCallback(WaitHandleExample::DoTask), handles[1]); int index = WaitHandle::WaitAny(handles); // The time shown below should match the shortest task. Console::WriteLine("Task {0} finished first (time waited={1}).", index + 1, (DateTime::Now - timeInstance).TotalMilliseconds); } // This code produces the following sample output. // // Main thread is waiting for BOTH tasks to complete. // Performing a task for 7000 milliseconds. // Performing a task for 4000 milliseconds. // Both tasks are completed (time waited=7064.8052) // The main thread is waiting for either task to complete. // Performing a task for 2000 milliseconds. // Performing a task for 2000 milliseconds. // Task 1 finished first (time waited=2000.6528).

System.MarshalByRefObject
System.Threading.WaitHandle
System.Threading.EventWaitHandle
System.Threading.Mutex
System.Threading.Semaphore

この型は、スレッド セーフです。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


WaitHandle コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


WaitHandle フィールド
WaitHandle プロパティ
WaitHandle メソッド


名前 | 説明 | |
---|---|---|
![]() | Dispose | 派生クラスでオーバーライドされると、WaitHandle によって使用されているアンマネージ リソースを解放し、オプションでマネージ リソースも解放します。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |


WaitHandle メンバ
共有リソースへの排他アクセスの待機に使用するオペレーティング システム固有のオブジェクトをカプセル化します。
WaitHandle データ型で公開されるメンバを以下の表に示します。






名前 | 説明 | |
---|---|---|
![]() | Dispose | 派生クラスでオーバーライドされると、WaitHandle によって使用されているアンマネージ リソースを解放し、オプションでマネージ リソースも解放します。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |


- WaitHandleのページへのリンク