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


![]() |
---|
このクラスに適用される HostProtectionAttribute 属性の Resources プロパティの値は、Synchronization または ExternalThreading です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的には、アイコンをダブルクリック、コマンドを入力、またはブラウザに URL を入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラスのトピックまたは「SQL Server プログラミングとホスト保護属性」を参照してください。 |
EventWaitHandle クラスを使用すると、スレッドはシグナルを通じて相互に通信できます。通常、EventWaitHandle では、ブロックされていないスレッドが Set メソッドを呼び出して 1 つ以上のブロックされたスレッドを解放するまでの間、1 つ以上のスレッドがブロックされます。スレッドは、static (Visual Basic では Shared) のSystem.Threading.WaitHandle.SignalAndWait メソッドを呼び出すことにより、分割不可能な操作として、EventWaitHandle にシグナルを通知してからブロックされます。
シグナル状態になった EventWaitHandle の動作は、リセット モードに依存します。EventResetMode.AutoReset フラグを指定して作成された EventWaitHandle がシグナル状態になっている場合は、待機中のスレッドを 1 つ解放した後で自動的にリセットされます。EventResetMode.ManualReset フラグを指定して作成された EventWaitHandle がシグナル状態になっている場合は、対応する Reset メソッドが呼び出されるまでシグナル状態のままとなります。
自動リセット イベントは、リソースへの排他的なアクセスを提供します。待機しているスレッドがないときに自動リセット イベントがシグナル状態になっている場合は、1 つのスレッドが待機を試みるまでシグナル状態のままとなります。このイベントはスレッドを解放した直後にリセットされ、後続のスレッドをブロックします。
手動リセットはゲートのようなものです。イベントがシグナル状態でないときは、イベントを待機しているスレッドはブロックされます。イベントがシグナル状態になっているときは、待機中のすべてのスレッドが解放され、対応する Reset メソッドが呼び出されるまでイベントがシグナル状態のままになります (つまり、後続の待機スレッドはブロックされません)。手動リセット イベントは、1 つのスレッドが処理を完了しないと他のスレッドが続行できない場合に便利です。
EventWaitHandle オブジェクトは、static (Visual Basic では Shared) の System.Threading.WaitHandle.WaitAll メソッドおよび System.Threading.WaitHandle.WaitAny メソッドと組み合わせても使用できます。
スレッドの同期機構の詳細については、「EventWaitHandle、AutoResetEvent、および ManualResetEvent」を参照してください。

SignalAndWait(WaitHandle,WaitHandle) メソッド オーバーロードを使用して、メイン スレッドがブロックされているスレッドにシグナルを送信し、そのスレッドがタスクを完了するまで待機できるようにするコード例を次に示します。
この例では 5 つのスレッドを起動し、それらのスレッドを EventResetMode.AutoReset フラグを指定して作成された EventWaitHandle でブロックした後、ユーザーが Enter キーを押すたびにスレッドが 1 つずつ解放されるようにします。次に、別の 5 つのスレッドをキューに置き、EventResetMode.ManualReset フラグを指定して作成された EventWaitHandle を使用して、これらのスレッドをすべて解放します。
Imports System 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
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(); } }
using namespace System; using namespace System::Threading; public ref class Example { private: // The EventWaitHandle used to demonstrate the difference // between AutoReset and ManualReset synchronization events. // 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. // static __int64 threadCount = 0; // An AutoReset event that allows the main thread to block // until an exiting thread has decremented the count. // static EventWaitHandle^ clearCount = gcnew EventWaitHandle( false,EventResetMode::AutoReset ); public: [MTAThread] static void main() { // Create an AutoReset EventWaitHandle. // ewh = gcnew 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 = gcnew Thread( gcnew 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( threadCount ) < 5 ) { Thread::Sleep( 500 ); } // Release one thread each time the user presses ENTER, // until all threads have been released. // while ( Interlocked::Read( threadCount ) > 0 ) { Console::WriteLine( L"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 = gcnew EventWaitHandle( false,EventResetMode::ManualReset ); // Create and start five more numbered threads. // for ( int i = 0; i <= 4; i++ ) { Thread^ t = gcnew Thread( gcnew ParameterizedThreadStart( ThreadProc ) ); t->Start( i ); } // Wait until all the threads have started and blocked. // while ( Interlocked::Read( threadCount ) < 5 ) { Thread::Sleep( 500 ); } // Because the EventWaitHandle was created with // ManualReset mode, signaling it releases all the // waiting threads. // Console::WriteLine( L"Press ENTER to release the waiting threads." ); Console::ReadLine(); ewh->Set(); } static void ThreadProc( Object^ data ) { int index = static_cast<Int32>(data); Console::WriteLine( L"Thread {0} blocks.", data ); // Increment the count of blocked threads. Interlocked::Increment( threadCount ); // Wait on the EventWaitHandle. ewh->WaitOne(); Console::WriteLine( L"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(); } };
import System.*; import 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); /** @attribute MTAThread() */ public static void main(String[] args) { // 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++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ThreadProc)); t.Start((Int32)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) { System.Threading.Thread.Sleep(500); } // 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); } 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++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ThreadProc)); t.Start((Int32)i); } // Wait until all the threads have started and blocked. // while (Interlocked.Read(threadCount) < 5) { System.Threading.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(); } //main public static void ThreadProc(Object data) { int index = System.Convert.ToInt32(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(); } //ThreadProc } //Example

System.MarshalByRefObject
System.Threading.WaitHandle
System.Threading.EventWaitHandle
System.Threading.AutoResetEvent
System.Threading.ManualResetEvent


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


EventWaitHandle コンストラクタ (Boolean, EventResetMode, String)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim initialState As Boolean Dim mode As EventResetMode Dim name As String Dim instance As New EventWaitHandle(initialState, mode, name)


name が null 参照 (Visual Basic では Nothing) または空の文字列の場合、ローカル EventWaitHandle が作成されます。
name パラメータで指定された名前を持つシステム イベントが既に存在する場合、initialState パラメータは無視されます。
![]() |
---|
名前付きシステム イベントにこのコンストラクタを使用するときは、initialState に false を指定します。このコンストラクタには名前付きシステム イベントが作成されたかどうかを確認する方法がないため、名前付きシステム イベントの状態を知ることができません。名前付きシステム イベントが作成されたかどうかを確認するには、EventWaitHandle(Boolean,EventResetMode,String,Boolean) コンストラクタまたは EventWaitHandle(Boolean,EventResetMode,String,Boolean,EventWaitHandleSecurity) コンストラクタを使用します。 |
イベントの初期状態が非シグナル状態の場合、イベントを待機するスレッドはブロックされます。イベントの初期状態がシグナル状態で、ManualReset フラグが mode に指定されている場合、イベントを待機するスレッドはブロックされません。イベントの初期状態がシグナル状態で、mode が AutoReset である場合、イベントを待機している最初のスレッドはすぐに解放されますが、その後イベントがリセットされ、後続のスレッドはブロックされます。


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


EventWaitHandle コンストラクタ (Boolean, EventResetMode)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim initialState As Boolean Dim mode As EventResetMode Dim instance As New EventWaitHandle(initialState, mode)

イベントの初期状態が非シグナル状態の場合、イベントを待機するスレッドはブロックされます。イベントの初期状態がシグナル状態で、ManualReset フラグが mode に指定されている場合、イベントを待機するスレッドはブロックされません。イベントの初期状態がシグナル状態で、mode が AutoReset である場合、イベントを待機している最初のスレッドはすぐに解放されますが、その後イベントがリセットされ、後続のスレッドはブロックされます。

SignalAndWait(WaitHandle,WaitHandle) メソッド オーバーロードを使用して、メイン スレッドがブロックされているスレッドにシグナルを送信し、そのスレッドがタスクを完了するまで待機できるようにするコード例を次に示します。
この例では 5 つのスレッドを起動し、それらのスレッドを EventResetMode.AutoReset フラグを指定して作成された EventWaitHandle でブロックした後、ユーザーが Enter キーを押すたびにスレッドが 1 つずつ解放されるようにします。次に、別の 5 つのスレッドをキューに置き、EventResetMode.ManualReset フラグを指定して作成された EventWaitHandle を使用して、これらのスレッドをすべて解放します。
Imports System 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
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(); } }
using namespace System; using namespace System::Threading; public ref class Example { private: // The EventWaitHandle used to demonstrate the difference // between AutoReset and ManualReset synchronization events. // 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. // static __int64 threadCount = 0; // An AutoReset event that allows the main thread to block // until an exiting thread has decremented the count. // static EventWaitHandle^ clearCount = gcnew EventWaitHandle( false,EventResetMode::AutoReset ); public: [MTAThread] static void main() { // Create an AutoReset EventWaitHandle. // ewh = gcnew 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 = gcnew Thread( gcnew 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( threadCount ) < 5 ) { Thread::Sleep( 500 ); } // Release one thread each time the user presses ENTER, // until all threads have been released. // while ( Interlocked::Read( threadCount ) > 0 ) { Console::WriteLine( L"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 = gcnew EventWaitHandle( false,EventResetMode::ManualReset ); // Create and start five more numbered threads. // for ( int i = 0; i <= 4; i++ ) { Thread^ t = gcnew Thread( gcnew ParameterizedThreadStart( ThreadProc ) ); t->Start( i ); } // Wait until all the threads have started and blocked. // while ( Interlocked::Read( threadCount ) < 5 ) { Thread::Sleep( 500 ); } // Because the EventWaitHandle was created with // ManualReset mode, signaling it releases all the // waiting threads. // Console::WriteLine( L"Press ENTER to release the waiting threads." ); Console::ReadLine(); ewh->Set(); } static void ThreadProc( Object^ data ) { int index = static_cast<Int32>(data); Console::WriteLine( L"Thread {0} blocks.", data ); // Increment the count of blocked threads. Interlocked::Increment( threadCount ); // Wait on the EventWaitHandle. ewh->WaitOne(); Console::WriteLine( L"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(); } };
import System.*; import 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); /** @attribute MTAThread() */ public static void main(String[] args) { // 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++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ThreadProc)); t.Start((Int32)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) { System.Threading.Thread.Sleep(500); } // 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); } 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++) { System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(ThreadProc)); t.Start((Int32)i); } // Wait until all the threads have started and blocked. // while (Interlocked.Read(threadCount) < 5) { System.Threading.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(); } //main public static void ThreadProc(Object data) { int index = System.Convert.ToInt32(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(); } //ThreadProc } //Example

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


EventWaitHandle コンストラクタ (Boolean, EventResetMode, String, Boolean)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ initialState As Boolean, _ mode As EventResetMode, _ name As String, _ <OutAttribute> ByRef createdNew As Boolean _ )
Dim initialState As Boolean Dim mode As EventResetMode Dim name As String Dim createdNew As Boolean Dim instance As New EventWaitHandle(initialState, mode, name, createdNew)
public: EventWaitHandle ( bool initialState, EventResetMode mode, String^ name, [OutAttribute] bool% createdNew )
public EventWaitHandle ( boolean initialState, EventResetMode mode, String name, /** @attribute OutAttribute() */ /** @ref */ boolean createdNew )
public function EventWaitHandle ( initialState : boolean, mode : EventResetMode, name : String, createdNew : boolean )


name パラメータで指定された名前を持つシステム イベントが既に存在する場合、initialState パラメータは無視されます。このコンストラクタを呼び出した後、ref パラメータ (Visual Basic では ByRef パラメータ) createdNew に指定された変数の値を使用して、名前付きシステム イベントが既に存在していたか、または作成されたかどうかを確認します。
イベントの初期状態が非シグナル状態の場合、イベントを待機するスレッドはブロックされます。イベントの初期状態がシグナル状態で、ManualReset フラグが mode に指定されている場合、イベントを待機するスレッドはブロックされません。イベントの初期状態がシグナル状態で、mode が AutoReset の場合、イベントを待機している最初のスレッドはすぐに解放されますが、その後イベントがリセットされ、後続のスレッドはブロックされます。


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


EventWaitHandle コンストラクタ

名前 | 説明 |
---|---|
EventWaitHandle (Boolean, EventResetMode) | 待機ハンドルの初期状態をシグナル状態に設定するかどうか、および、待機ハンドルが自動的にリセットされるかまたは手動でリセットされるかを指定して、EventWaitHandle クラスの新しいインスタンスを初期化します。 |
EventWaitHandle (Boolean, EventResetMode, String) | この呼び出しの結果として待機ハンドルが作成された場合に待機ハンドルの初期状態をシグナル状態に設定するかどうか、待機ハンドルが自動的にリセットされるかまたは手動でリセットされるか、およびシステムの同期イベントの名前を指定して、EventWaitHandle クラスの新しいインスタンスを初期化します。 |
EventWaitHandle (Boolean, EventResetMode, String, Boolean) | この呼び出しの結果として待機ハンドルが作成された場合に待機ハンドルの初期状態をシグナル状態に設定するかどうか、待機ハンドルが自動的にリセットされるかまたは手動でリセットされるか、システム同期イベントの名前、および、呼び出し後の値によって名前付きイベントが作成されたかどうかを示すブール変数を指定して、EventWaitHandle クラスの新しいインスタンスを初期化します。 |
EventWaitHandle (Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity) | この呼び出しの結果として待機ハンドルが作成された場合に待機ハンドルの初期状態をシグナル状態に設定するかどうか、待機ハンドルが自動的にリセットされるかまたは手動でリセットされるか、システム同期イベントの名前、呼び出し後の値によって名前付きイベントが作成されたかどうかを示すブール変数、および名前付きシステム イベントが作成された場合に適用するアクセス制御セキュリティを指定して、EventWaitHandle クラスの新しいインスタンスを初期化します。 |

関連項目
EventWaitHandle クラスEventWaitHandle メンバ
System.Threading 名前空間
その他の技術情報
EventWaitHandle、AutoResetEvent、および ManualResetEventEventWaitHandle コンストラクタ (Boolean, EventResetMode, String, Boolean, EventWaitHandleSecurity)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Sub New ( _ initialState As Boolean, _ mode As EventResetMode, _ name As String, _ <OutAttribute> ByRef createdNew As Boolean, _ eventSecurity As EventWaitHandleSecurity _ )
Dim initialState As Boolean Dim mode As EventResetMode Dim name As String Dim createdNew As Boolean Dim eventSecurity As EventWaitHandleSecurity Dim instance As New EventWaitHandle(initialState, mode, name, createdNew, eventSecurity)
public EventWaitHandle ( bool initialState, EventResetMode mode, string name, out bool createdNew, EventWaitHandleSecurity eventSecurity )
public: EventWaitHandle ( bool initialState, EventResetMode mode, String^ name, [OutAttribute] bool% createdNew, EventWaitHandleSecurity^ eventSecurity )
public EventWaitHandle ( boolean initialState, EventResetMode mode, String name, /** @attribute OutAttribute() */ /** @ref */ boolean createdNew, EventWaitHandleSecurity eventSecurity )
public function EventWaitHandle ( initialState : boolean, mode : EventResetMode, name : String, createdNew : boolean, eventSecurity : EventWaitHandleSecurity )


このコンストラクタは、名前付きシステム イベントの作成時にイベントにアクセス制御セキュリティを適用して、他のコードがイベントを制御できないようにするために使用します。
このコンストラクタは、システム イベントを表す EventWaitHandle オブジェクトを初期化します。同じシステム イベントを表す複数の EventWaitHandle オブジェクトを作成できます。
システム イベントが存在しない場合、指定したアクセス制御セキュリティでシステム イベントが作成されます。イベントが存在する場合には、指定したアクセス制御セキュリティは無視されます。
![]() |
---|
eventSecurity が現在のユーザーに対して一部のアクセス権を拒否していたり、アクセス権を付与できなかったりした場合でも、呼び出し元は新しく作成した EventWaitHandle オブジェクトを完全に制御します。ただし、現在のユーザーがコンストラクタまたは OpenExisting メソッドを使用して、同じ名前付きイベントを表す別の EventWaitHandle オブジェクトを取得しようとした場合は、Windows のアクセス制御セキュリティが適用されます。 |
name パラメータで指定された名前を持つシステム イベントが既に存在する場合、initialState パラメータは無視されます。このコンストラクタを呼び出した後、ref パラメータ (Visual Basic では ByRef パラメータ) createdNew に指定された変数の値を使用して、名前付きシステム イベントが既に存在していたか、または作成されたかどうかを確認します。
イベントの初期状態が非シグナル状態の場合、イベントを待機するスレッドはブロックされます。イベントの初期状態がシグナル状態で、ManualReset フラグが mode に指定されている場合、イベントを待機するスレッドはブロックされません。イベントの初期状態がシグナル状態で、mode が AutoReset の場合、イベントを待機している最初のスレッドはすぐに解放されますが、その後イベントがリセットされ、後続のスレッドはブロックされます。

アクセス制御セキュリティを使用した名前付きシステム イベントのプロセス間の動作をデモンストレーションするコード例を次に示します。この例では、OpenExisting(String) メソッド オーバーロードを使用して、名前付きイベントが存在するかどうかをテストしています。
イベントが存在しない場合、初期所有権と、現在のユーザーによるイベントの使用を拒否しイベントに対する読み取りと変更のアクセス許可のみを付与するアクセス制御セキュリティでイベントが作成されます。
2 つのコマンド ウィンドウからコンパイルした例を実行すると、2 番目のコピーは OpenExisting(String) の呼び出しでアクセス違反例外をスローします。例外がキャッチされると、OpenExisting(String,EventWaitHandleRights) メソッド オーバーロードを使用し、アクセス許可の読み取りと変更に必要な権限で、イベントを待機します。
アクセス許可が変更された後、待機とシグナル通知に必要な権限でイベントが開かれます。3 つ目のコマンド ウィンドウからコンパイル済みの例を実行する場合、新しいアクセス許可を使用して実行されます。
Imports System Imports System.Threading Imports System.Security.AccessControl Friend Class Example <MTAThread> _ Friend Shared Sub Main() Const ewhName As String = "EventWaitHandleExample5" Dim ewh As EventWaitHandle = Nothing Dim doesNotExist as Boolean = False Dim unauthorized As Boolean = False ' The value of this variable is set by the event ' constructor. It is True if the named system event was ' created, and False if the named event already existed. ' Dim wasCreated As Boolean ' Attempt to open the named event. Try ' Open the event with (EventWaitHandleRights.Synchronize ' Or EventWaitHandleRights.Modify), to wait on and ' signal the named event. ' ewh = EventWaitHandle.OpenExisting(ewhName) Catch ex As WaitHandleCannotBeOpenedException Console.WriteLine("Named event does not exist.") doesNotExist = True Catch ex As UnauthorizedAccessException Console.WriteLine("Unauthorized access: {0}", ex.Message) unauthorized = True End Try ' There are three cases: (1) The event does not exist. ' (2) The event exists, but the current user doesn't ' have access. (3) The event exists and the user has ' access. ' If doesNotExist Then ' The event does not exist, so create it. ' Create an access control list (ACL) that denies the ' current user the right to wait on or signal the ' event, but allows the right to read and change ' security information for the event. ' Dim user As String = Environment.UserDomainName _ & "\" & Environment.UserName Dim ewhSec As New EventWaitHandleSecurity() Dim rule As New EventWaitHandleAccessRule(user, _ EventWaitHandleRights.Synchronize Or _ EventWaitHandleRights.Modify, _ AccessControlType.Deny) ewhSec.AddAccessRule(rule) rule = New EventWaitHandleAccessRule(user, _ EventWaitHandleRights.ReadPermissions Or _ EventWaitHandleRights.ChangePermissions, _ AccessControlType.Allow) ewhSec.AddAccessRule(rule) ' Create an EventWaitHandle object that represents ' the system event named by the constant 'ewhName', ' initially signaled, with automatic reset, and with ' the specified security access. The Boolean value that ' indicates creation of the underlying system object ' is placed in wasCreated. ' ewh = New EventWaitHandle(True, _ EventResetMode.AutoReset, ewhName, _ wasCreated, ewhSec) ' If the named system event was created, it can be ' used by the current instance of this program, even ' though the current user is denied access. The current ' program owns the event. Otherwise, exit the program. ' If wasCreated Then Console.WriteLine("Created the named event.") Else Console.WriteLine("Unable to create the event.") Return End If ElseIf unauthorized Then ' Open the event to read and change the access control ' security. The access control security defined above ' allows the current user to do this. ' Try ewh = EventWaitHandle.OpenExisting(ewhName, _ EventWaitHandleRights.ReadPermissions Or _ EventWaitHandleRights.ChangePermissions) ' Get the current ACL. This requires ' EventWaitHandleRights.ReadPermissions. Dim ewhSec As EventWaitHandleSecurity = _ ewh.GetAccessControl() Dim user As String = Environment.UserDomainName _ & "\" & Environment.UserName ' First, the rule that denied the current user ' the right to enter and release the event must ' be removed. Dim rule As New EventWaitHandleAccessRule(user, _ EventWaitHandleRights.Synchronize Or _ EventWaitHandleRights.Modify, _ AccessControlType.Deny) ewhSec.RemoveAccessRule(rule) ' Now grant the user the correct rights. ' rule = New EventWaitHandleAccessRule(user, _ EventWaitHandleRights.Synchronize Or _ EventWaitHandleRights.Modify, _ AccessControlType.Allow) ewhSec.AddAccessRule(rule) ' Update the ACL. This requires ' EventWaitHandleRights.ChangePermissions. ewh.SetAccessControl(ewhSec) Console.WriteLine("Updated event security.") ' Open the event with (EventWaitHandleRights.Synchronize ' Or EventWaitHandleRights.Modify), the rights required ' to wait on and signal the event. ' ewh = EventWaitHandle.OpenExisting(ewhName) Catch ex As UnauthorizedAccessException Console.WriteLine("Unable to change permissions: {0}", _ ex.Message) Return End Try End If ' Wait on the event, and hold it until the program ' exits. ' Try Console.WriteLine("Wait on the event.") ewh.WaitOne() Console.WriteLine("Event was signaled.") Console.WriteLine("Press the Enter key to signal the event and exit.") Console.ReadLine() Catch ex As UnauthorizedAccessException Console.WriteLine("Unauthorized access: {0}", _ ex.Message) Finally ewh.Set() End Try End Sub End Class
using System; using System.Threading; using System.Security.AccessControl; internal class Example { internal static void Main() { const string ewhName = "EventWaitHandleExample5"; EventWaitHandle ewh = null; bool doesNotExist = false; bool unauthorized = false; // The value of this variable is set by the event // constructor. It is true if the named system event was // created, and false if the named event already existed. // bool wasCreated; // Attempt to open the named event. try { // Open the event with (EventWaitHandleRights.Synchronize // | EventWaitHandleRights.Modify), to wait on and // signal the named event. // ewh = EventWaitHandle.OpenExisting(ewhName); } catch (WaitHandleCannotBeOpenedException) { Console.WriteLine("Named event does not exist."); doesNotExist = true; } catch (UnauthorizedAccessException ex) { Console.WriteLine("Unauthorized access: {0}", ex.Message); unauthorized = true; } // There are three cases: (1) The event does not exist. // (2) The event exists, but the current user doesn't // have access. (3) The event exists and the user has // access. // if (doesNotExist) { // The event does not exist, so create it. // Create an access control list (ACL) that denies the // current user the right to wait on or signal the // event, but allows the right to read and change // security information for the event. // string user = Environment.UserDomainName + "\\" + Environment.UserName; EventWaitHandleSecurity ewhSec = new EventWaitHandleSecurity(); EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Deny); ewhSec.AddAccessRule(rule); rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.ReadPermissions | EventWaitHandleRights.ChangePermissions, AccessControlType.Allow); ewhSec.AddAccessRule(rule); // Create an EventWaitHandle object that represents // the system event named by the constant 'ewhName', // initially signaled, with automatic reset, and with // the specified security access. The Boolean value that // indicates creation of the underlying system object // is placed in wasCreated. // ewh = new EventWaitHandle(true, EventResetMode.AutoReset, ewhName, out wasCreated, ewhSec); // If the named system event was created, it can be // used by the current instance of this program, even // though the current user is denied access. The current // program owns the event. Otherwise, exit the program. // if (wasCreated) { Console.WriteLine("Created the named event."); } else { Console.WriteLine("Unable to create the event."); return; } } else if (unauthorized) { // Open the event to read and change the access control // security. The access control security defined above // allows the current user to do this. // try { ewh = EventWaitHandle.OpenExisting(ewhName, EventWaitHandleRights.ReadPermissions | EventWaitHandleRights.ChangePermissions); // Get the current ACL. This requires // EventWaitHandleRights.ReadPermissions. EventWaitHandleSecurity ewhSec = ewh.GetAccessControl(); string user = Environment.UserDomainName + "\\" + Environment.UserName; // First, the rule that denied the current user // the right to enter and release the event must // be removed. EventWaitHandleAccessRule rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Deny); ewhSec.RemoveAccessRule(rule); // Now grant the user the correct rights. // rule = new EventWaitHandleAccessRule(user, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, AccessControlType.Allow); ewhSec.AddAccessRule(rule); // Update the ACL. This requires // EventWaitHandleRights.ChangePermissions. ewh.SetAccessControl(ewhSec); Console.WriteLine("Updated event security."); // Open the event with (EventWaitHandleRights.Synchronize // | EventWaitHandleRights.Modify), the rights required // to wait on and signal the event. // ewh = EventWaitHandle.OpenExisting(ewhName); } catch (UnauthorizedAccessException ex) { Console.WriteLine("Unable to change permissions: {0}", ex.Message); return; } } // Wait on the event, and hold it until the program // exits. // try { Console.WriteLine("Wait on the event."); ewh.WaitOne(); Console.WriteLine("Event was signaled."); Console.WriteLine("Press the Enter key to signal the event and exit."); Console.ReadLine(); } catch (UnauthorizedAccessException ex) { Console.WriteLine("Unauthorized access: {0}", ex.Message); } finally { ewh.Set(); } } }
using namespace System; using namespace System::Threading; using namespace System::Security::AccessControl; using namespace System::Security::Permissions; public ref class Example { public: [SecurityPermissionAttribute(SecurityAction::Demand,Flags=SecurityPermissionFlag::UnmanagedCode)] static void Main() { String^ ewhName = L"EventWaitHandleExample5"; EventWaitHandle^ ewh = nullptr; bool doesNotExist = false; bool unauthorized = false; // The value of this variable is set by the event // constructor. It is true if the named system event was // created, and false if the named event already existed. // bool wasCreated; // Attempt to open the named event. try { // Open the event with (EventWaitHandleRights.Synchronize // | EventWaitHandleRights.Modify), to wait on and // signal the named event. // ewh = EventWaitHandle::OpenExisting( ewhName ); } catch ( WaitHandleCannotBeOpenedException^ ) { Console::WriteLine( L"Named event does not exist." ); doesNotExist = true; } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unauthorized access: {0}", ex->Message ); unauthorized = true; } // There are three cases: (1) The event does not exist. // (2) The event exists, but the current user doesn't // have access. (3) The event exists and the user has // access. // if ( doesNotExist ) { // The event does not exist, so create it. // Create an access control list (ACL) that denies the // current user the right to wait on or signal the // event, but allows the right to read and change // security information for the event. // String^ user = String::Concat( Environment::UserDomainName, L"\\" , Environment::UserName ); EventWaitHandleSecurity^ ewhSec = gcnew EventWaitHandleSecurity; //following constructor fails EventWaitHandleAccessRule^ rule = gcnew EventWaitHandleAccessRule( user, static_cast<EventWaitHandleRights>( EventWaitHandleRights::Synchronize | EventWaitHandleRights::Modify), AccessControlType::Deny ); ewhSec->AddAccessRule( rule ); rule = gcnew EventWaitHandleAccessRule( user, static_cast<EventWaitHandleRights>( EventWaitHandleRights::ReadPermissions | EventWaitHandleRights::ChangePermissions), AccessControlType::Allow ); ewhSec->AddAccessRule( rule ); // Create an EventWaitHandle object that represents // the system event named by the constant 'ewhName', // initially signaled, with automatic reset, and with // the specified security access. The Boolean value that // indicates creation of the underlying system object // is placed in wasCreated. // ewh = gcnew EventWaitHandle( true, EventResetMode::AutoReset, ewhName, wasCreated, ewhSec ); // If the named system event was created, it can be // used by the current instance of this program, even // though the current user is denied access. The current // program owns the event. Otherwise, exit the program. // if ( wasCreated ) { Console::WriteLine( L"Created the named event." ); } else { Console::WriteLine( L"Unable to create the event." ); return; } } else if ( unauthorized ) { // Open the event to read and change the access control // security. The access control security defined above // allows the current user to do this. // try { ewh = EventWaitHandle::OpenExisting( ewhName, static_cast<EventWaitHandleRights>( EventWaitHandleRights::ReadPermissions | EventWaitHandleRights::ChangePermissions) ); // Get the current ACL. This requires // EventWaitHandleRights.ReadPermissions. EventWaitHandleSecurity^ ewhSec = ewh->GetAccessControl(); String^ user = String::Concat( Environment::UserDomainName, L"\\" , Environment::UserName ); // First, the rule that denied the current user // the right to enter and release the event must // be removed. EventWaitHandleAccessRule^ rule = gcnew EventWaitHandleAccessRule( user, static_cast<EventWaitHandleRights>( EventWaitHandleRights::Synchronize | EventWaitHandleRights::Modify), AccessControlType::Deny ); ewhSec->RemoveAccessRule( rule ); // Now grant the user the correct rights. // rule = gcnew EventWaitHandleAccessRule( user, static_cast<EventWaitHandleRights>( EventWaitHandleRights::Synchronize | EventWaitHandleRights::Modify), AccessControlType::Allow ); ewhSec->AddAccessRule( rule ); // Update the ACL. This requires // EventWaitHandleRights.ChangePermissions. ewh->SetAccessControl( ewhSec ); Console::WriteLine( L"Updated event security." ); // Open the event with (EventWaitHandleRights.Synchronize // | EventWaitHandleRights.Modify), the rights required // to wait on and signal the event. // ewh = EventWaitHandle::OpenExisting( ewhName ); } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unable to change permissions: {0}", ex->Message ); return; } } // Wait on the event, and hold it until the program // exits. // try { Console::WriteLine( L"Wait on the event." ); ewh->WaitOne(); Console::WriteLine( L"Event was signaled." ); Console::WriteLine( L"Press the Enter key to signal the event and exit." ); Console::ReadLine(); } catch ( UnauthorizedAccessException^ ex ) { Console::WriteLine( L"Unauthorized access: {0}", ex->Message ); } finally { ewh->Set(); } } }; int main() { Example::Main(); }


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


EventWaitHandle プロパティ

名前 | 説明 | |
---|---|---|
![]() | Handle | ネイティブ オペレーティング システム ハンドルを取得または設定します。 ( WaitHandle から継承されます。) |
![]() | SafeWaitHandle | ネイティブ オペレーティング システム ハンドルを取得または設定します。 ( WaitHandle から継承されます。) |

EventWaitHandle メソッド


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

EventWaitHandle メンバ
EventWaitHandle データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Handle | ネイティブ オペレーティング システム ハンドルを取得または設定します。(WaitHandle から継承されます。) |
![]() | SafeWaitHandle | ネイティブ オペレーティング システム ハンドルを取得または設定します。(WaitHandle から継承されます。) |


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

- EventWaitHandleのページへのリンク