EventWaitHandle クラス

定義

スレッド同期イベントを表します。

public ref class EventWaitHandle : System::Threading::WaitHandle
public class EventWaitHandle : System.Threading.WaitHandle
[System.Runtime.InteropServices.ComVisible(true)]
public class EventWaitHandle : System.Threading.WaitHandle
type EventWaitHandle = class
    inherit WaitHandle
[<System.Runtime.InteropServices.ComVisible(true)>]
type EventWaitHandle = class
    inherit WaitHandle
Public Class EventWaitHandle
Inherits WaitHandle
継承
EventWaitHandle
継承
派生
属性

次のコード例では、 SignalAndWait(WaitHandle, WaitHandle) メソッドのオーバーロードを使用して、メイン スレッドがブロックされたスレッドを通知し、スレッドがタスクを完了するまで待機できるようにします。

この例では、5 つのスレッドを開始し、EventResetMode.AutoReset フラグで作成されたEventWaitHandleでブロックし、ユーザーが Enter キーを押すたびに 1 つのスレッドを解放します。 この例では、別の 5 つのスレッドをキューに入れ、EventResetMode.ManualReset フラグで作成されたEventWaitHandleを使用してすべて解放します。

using System;
using System.Threading;

public class Example
{
    // The EventWaitHandle used to demonstrate the difference
    // between AutoReset and ManualReset synchronization events.
    //
    private static EventWaitHandle ewh;

    // A counter to make sure all threads are started and
    // blocked before any are released. A Long is used to show
    // the use of the 64-bit Interlocked methods.
    //
    private static long threadCount = 0;

    // An AutoReset event that allows the main thread to block
    // until an exiting thread has decremented the count.
    //
    private static EventWaitHandle clearCount = 
        new EventWaitHandle(false, EventResetMode.AutoReset);

    [MTAThread]
    public static void Main()
    {
        // Create an AutoReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.AutoReset);

        // Create and start five numbered threads. Use the
        // ParameterizedThreadStart delegate, so the thread
        // number can be passed as an argument to the Start 
        // method.
        for (int i = 0; i <= 4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }

        // Wait until all the threads have started and blocked.
        // When multiple threads use a 64-bit value on a 32-bit
        // system, you must access the value through the
        // Interlocked class to guarantee thread safety.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }

        // Release one thread each time the user presses ENTER,
        // until all threads have been released.
        //
        while (Interlocked.Read(ref threadCount) > 0)
        {
            Console.WriteLine("Press ENTER to release a waiting thread.");
            Console.ReadLine();

            // SignalAndWait signals the EventWaitHandle, which
            // releases exactly one thread before resetting, 
            // because it was created with AutoReset mode. 
            // SignalAndWait then blocks on clearCount, to 
            // allow the signaled thread to decrement the count
            // before looping again.
            //
            WaitHandle.SignalAndWait(ewh, clearCount);
        }
        Console.WriteLine();

        // Create a ManualReset EventWaitHandle.
        //
        ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

        // Create and start five more numbered threads.
        //
        for(int i=0; i<=4; i++)
        {
            Thread t = new Thread(
                new ParameterizedThreadStart(ThreadProc)
            );
            t.Start(i);
        }

        // Wait until all the threads have started and blocked.
        //
        while (Interlocked.Read(ref threadCount) < 5)
        {
            Thread.Sleep(500);
        }

        // Because the EventWaitHandle was created with
        // ManualReset mode, signaling it releases all the
        // waiting threads.
        //
        Console.WriteLine("Press ENTER to release the waiting threads.");
        Console.ReadLine();
        ewh.Set();
    }

    public static void ThreadProc(object data)
    {
        int index = (int) data;

        Console.WriteLine("Thread {0} blocks.", data);
        // Increment the count of blocked threads.
        Interlocked.Increment(ref threadCount);

        // Wait on the EventWaitHandle.
        ewh.WaitOne();

        Console.WriteLine("Thread {0} exits.", data);
        // Decrement the count of blocked threads.
        Interlocked.Decrement(ref threadCount);

        // After signaling ewh, the main thread blocks on
        // clearCount until the signaled thread has 
        // decremented the count. Signal it now.
        //
        clearCount.Set();
    }
}
Imports System.Threading

Public Class Example

    ' The EventWaitHandle used to demonstrate the difference
    ' between AutoReset and ManualReset synchronization events.
    '
    Private Shared ewh As EventWaitHandle

    ' A counter to make sure all threads are started and
    ' blocked before any are released. A Long is used to show
    ' the use of the 64-bit Interlocked methods.
    '
    Private Shared threadCount As Long = 0

    ' An AutoReset event that allows the main thread to block
    ' until an exiting thread has decremented the count.
    '
    Private Shared clearCount As New EventWaitHandle(False, _
        EventResetMode.AutoReset)

    <MTAThread> _
    Public Shared Sub Main()

        ' Create an AutoReset EventWaitHandle.
        '
        ewh = New EventWaitHandle(False, EventResetMode.AutoReset)

        ' Create and start five numbered threads. Use the
        ' ParameterizedThreadStart delegate, so the thread
        ' number can be passed as an argument to the Start 
        ' method.
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked.
        ' When multiple threads use a 64-bit value on a 32-bit
        ' system, you must access the value through the
        ' Interlocked class to guarantee thread safety.
        '
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While

        ' Release one thread each time the user presses ENTER,
        ' until all threads have been released.
        '
        While Interlocked.Read(threadCount) > 0
            Console.WriteLine("Press ENTER to release a waiting thread.")
            Console.ReadLine()

            ' SignalAndWait signals the EventWaitHandle, which
            ' releases exactly one thread before resetting, 
            ' because it was created with AutoReset mode. 
            ' SignalAndWait then blocks on clearCount, to 
            ' allow the signaled thread to decrement the count
            ' before looping again.
            '
            WaitHandle.SignalAndWait(ewh, clearCount)
        End While
        Console.WriteLine()

        ' Create a ManualReset EventWaitHandle.
        '
        ewh = New EventWaitHandle(False, EventResetMode.ManualReset)

        ' Create and start five more numbered threads.
        '
        For i As Integer = 0 To 4
            Dim t As New Thread(AddressOf ThreadProc)
            t.Start(i)
        Next i

        ' Wait until all the threads have started and blocked.
        '
        While Interlocked.Read(threadCount) < 5
            Thread.Sleep(500)
        End While

        ' Because the EventWaitHandle was created with
        ' ManualReset mode, signaling it releases all the
        ' waiting threads.
        '
        Console.WriteLine("Press ENTER to release the waiting threads.")
        Console.ReadLine()
        ewh.Set()
        
    End Sub

    Public Shared Sub ThreadProc(ByVal data As Object)
        Dim index As Integer = CInt(data)

        Console.WriteLine("Thread {0} blocks.", data)
        ' Increment the count of blocked threads.
        Interlocked.Increment(threadCount)

        ' Wait on the EventWaitHandle.
        ewh.WaitOne()

        Console.WriteLine("Thread {0} exits.", data)
        ' Decrement the count of blocked threads.
        Interlocked.Decrement(threadCount)

        ' After signaling ewh, the main thread blocks on
        ' clearCount until the signaled thread has 
        ' decremented the count. Signal it now.
        '
        clearCount.Set()
    End Sub
End Class

注釈

EventWaitHandle クラスを使用すると、スレッドはシグナル通知によって相互に通信できます。 通常、ブロック解除されたスレッドが Set メソッドを呼び出し、ブロックされたスレッドの 1 つ以上を解放するまで、1 つ以上のスレッドがEventWaitHandleでブロックされます。 スレッドは、EventWaitHandle を通知し、static (Visual Basic の Shared) WaitHandle.SignalAndWait メソッドを呼び出すことによってブロックできます。

Note

EventWaitHandle クラスは、名前付きシステム同期イベントへのアクセスを提供します。

通知された EventWaitHandle の動作は、リセット モードによって異なります。 EventResetMode.AutoReset フラグを使用して作成されたEventWaitHandleは、1 つの待機中のスレッドを解放した後、通知されると自動的にリセットされます。 EventWaitHandle フラグを使用して作成されたEventResetMode.ManualResetは、Reset メソッドが呼び出されるまでシグナル通知されます。

自動リセット イベントは、リソースへの排他的アクセスを提供します。 スレッドが待機していないときに自動リセット イベントが通知された場合、スレッドが待機を試みるまで通知されたままになります。 イベントによってスレッドが解放され、直ちにリセットされ、後続のスレッドがブロックされます。

手動リセット イベントはゲートのようなものです。 イベントが通知されない場合、イベントを待機するスレッドはブロックされます。 イベントが通知されると、すべての待機中のスレッドが解放され、その Reset メソッドが呼び出されるまで、イベントは通知されたままになります (つまり、後続の待機はブロックされません)。 手動リセット イベントは、あるスレッドが他のスレッドを続行する前にアクティビティを完了する必要がある場合に便利です。

EventWaitHandle オブジェクトは、static(Visual Basic の Shared) WaitHandle.WaitAll および WaitHandle.WaitAny メソッドで使用できます。

詳細については、同期プリミティブの概要に関する記事のスレッド 相互作用またはシグナル 通知に 関するセクションを 参照してください。

Caution

既定では、名前付きイベントは、それを作成したユーザーに制限されません。 他のユーザーは、イベントを不適切に設定またはリセットすることによってイベントを妨害するなど、イベントを開いて使用できる場合があります。 特定のユーザーへのアクセスを制限するには、コンストラクターのオーバーロードまたは EventWaitHandleAcl を使用し、名前付きイベントの作成時に EventWaitHandleSecurity を渡します。 信頼されていないユーザーがコードを実行している可能性があるシステムでは、アクセス制限なしで名前付きイベントを使用しないでください。

コンストラクター

名前 説明
EventWaitHandle(Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)

EventWaitHandle クラスの新しいインスタンスを初期化します。この呼び出しの結果として待機ハンドルが作成された場合に最初に通知されるかどうか、自動的にリセットされるか手動でリセットされるか、システム同期イベントの名前、呼び出し後の値が名前付きシステム イベントが作成されたかどうかを示すブール変数、および名前付きイベントが作成された場合に名前付きイベントに適用されるアクセス制御セキュリティを指定します。

EventWaitHandle(Boolean, EventResetMode, String, Boolean)

EventWaitHandle クラスの新しいインスタンスを初期化します。この呼び出しの結果として作成された待機ハンドルが最初に通知されるかどうか、自動または手動のどちらでリセットされるか、システム同期イベントの名前、および呼び出し後の値が名前付きシステム イベントが作成されたかどうかを示すブール変数を指定します。

EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions, Boolean)

EventWaitHandle クラスの新しいインスタンスを初期化します。この呼び出しの結果として待機ハンドルが作成された場合に最初に通知されるかどうか、自動的にリセットするか手動でリセットするか、システム同期イベントの名前、ユーザー スコープとセッション スコープのアクセスを設定するオプション、および呼び出し後の値が名前付きシステム イベントが作成されたかどうかを示すブール変数を指定します。

EventWaitHandle(Boolean, EventResetMode, String, NamedWaitHandleOptions)

EventWaitHandle クラスの新しいインスタンスを初期化します。この呼び出しの結果として作成された待機ハンドルが最初に通知されるかどうか、自動または手動のどちらでリセットされるか、システム同期イベントの名前、ユーザー スコープとセッション スコープのアクセスを設定するためのオプションを指定します。

EventWaitHandle(Boolean, EventResetMode, String)

EventWaitHandle クラスの新しいインスタンスを初期化します。この呼び出しの結果として作成された待機ハンドルが最初に通知されるかどうか、自動的にリセットされるか手動でリセットされるか、システム同期イベントの名前を指定します。

EventWaitHandle(Boolean, EventResetMode)

待機ハンドルが最初に通知されるかどうか、および待機ハンドルが自動的にリセットされるか手動でリセットされるかを指定して、 EventWaitHandle クラスの新しいインスタンスを初期化します。

フィールド

名前 説明
WaitTimeout

待機ハンドルが通知される前に、 WaitAny(WaitHandle[], Int32, Boolean) 操作がタイムアウトしたことを示します。 このフィールドは定数です。

(継承元 WaitHandle)

プロパティ

名前 説明
Handle
古い.
古い.

ネイティブ オペレーティング システム ハンドルを取得または設定します。

(継承元 WaitHandle)
SafeWaitHandle

ネイティブ オペレーティング システム ハンドルを取得または設定します。

(継承元 WaitHandle)

メソッド

名前 説明
Close()

現在の WaitHandleによって保持されているすべてのリソースを解放します。

(継承元 WaitHandle)
CreateObjRef(Type)

リモート オブジェクトとの通信に使用されるプロキシの生成に必要なすべての関連情報を含むオブジェクトを作成します。

(継承元 MarshalByRefObject)
Dispose()

WaitHandle クラスの現在のインスタンスで使用されているすべてのリソースを解放します。

(継承元 WaitHandle)
Dispose(Boolean)

派生クラスでオーバーライドされると、 WaitHandleによって使用されるアンマネージ リソースが解放され、必要に応じてマネージド リソースが解放されます。

(継承元 WaitHandle)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetAccessControl()

現在のEventWaitHandle オブジェクトによって表される名前付きシステム イベントのアクセス制御セキュリティを表すEventWaitHandleSecurity オブジェクトを取得します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
古い.

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
OpenExisting(String, EventWaitHandleRights)

指定した名前付き同期イベントが既に存在する場合は、目的のセキュリティ アクセスを使用して開きます。

OpenExisting(String, NamedWaitHandleOptions)

指定した名前付き同期イベントが既に存在する場合は開きます。 オプションが現在のユーザーのみに設定されている場合、オブジェクトのアクセス制御は呼び出し元のユーザーに対して検証されます。

OpenExisting(String)

指定した名前付き同期イベントが既に存在する場合は開きます。

Reset()

イベントの状態を非署名に設定し、スレッドをブロックします。

Set()

イベントの状態を signaled に設定し、1 つ以上の待機中のスレッドを続行できるようにします。

SetAccessControl(EventWaitHandleSecurity)

名前付きシステム イベントのアクセス制御セキュリティを設定します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
TryOpenExisting(String, EventWaitHandle)

指定した名前付き同期イベントが既に存在する場合は開き、操作が成功したかどうかを示す値を返します。

TryOpenExisting(String, EventWaitHandleRights, EventWaitHandle)

指定した名前付き同期イベントが既に存在する場合は、目的のセキュリティ アクセスを使用して開き、操作が成功したかどうかを示す値を返します。

TryOpenExisting(String, NamedWaitHandleOptions, EventWaitHandle)

指定した名前付き同期イベントが既に存在する場合は開き、操作が成功したかどうかを示す値を返します。 オプションが現在のユーザーのみに設定されている場合、オブジェクトのアクセス制御は呼び出し元のユーザーに対して検証されます。

WaitOne()

現在の WaitHandle がシグナルを受信するまで、現在のスレッドをブロックします。

(継承元 WaitHandle)
WaitOne(Int32, Boolean)

32 ビット符号付き整数を使用して時間間隔を指定し、待機前に同期ドメインを終了するかどうかを指定して、現在の WaitHandle がシグナルを受信するまで、現在のスレッドをブロックします。

(継承元 WaitHandle)
WaitOne(Int32)

現在の WaitHandle がシグナルを受信するまで、32 ビット符号付き整数を使用してミリ秒単位で時間間隔を指定するまで、現在のスレッドをブロックします。

(継承元 WaitHandle)
WaitOne(TimeSpan, Boolean)

TimeSpanを使用して時間間隔を指定し、待機前に同期ドメインを終了するかどうかを指定して、現在のインスタンスがシグナルを受信するまで、現在のスレッドをブロックします。

(継承元 WaitHandle)
WaitOne(TimeSpan)

TimeSpanを使用して時間間隔を指定して、現在のインスタンスがシグナルを受信するまで、現在のスレッドをブロックします。

(継承元 WaitHandle)

明示的なインターフェイスの実装

名前 説明
IDisposable.Dispose()

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

WaitHandleによって使用されるすべてのリソースを解放します。

(継承元 WaitHandle)

拡張メソッド

名前 説明
GetAccessControl(EventWaitHandle)

指定した handleのセキュリティ記述子を返します。

GetSafeWaitHandle(WaitHandle)

ネイティブ オペレーティング システムの待機ハンドルのセーフ ハンドルを取得します。

SetAccessControl(EventWaitHandle, EventWaitHandleSecurity)

指定したイベント待機ハンドルのセキュリティ記述子を設定します。

SetSafeWaitHandle(WaitHandle, SafeWaitHandle)

ネイティブ オペレーティング システムの待機ハンドルのセーフ ハンドルを設定します。

適用対象

スレッド セーフ

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

こちらもご覧ください