EventWaitHandle.Set メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As EventWaitHandle Dim returnValue As Boolean returnValue = instance.Set
正常に操作できた場合は true。それ以外の場合は false。

EventResetMode.AutoReset (AutoResetEvent を含む) が設定されている EventWaitHandle では、Set メソッドによってスレッドが 1 つ解放されます。待機中のスレッドがない場合、待機ハンドルは、任意のスレッドが待機を試みるかまたは Reset メソッドが呼び出されるまでの間、シグナル状態のままになります。
EventResetMode.ManualReset (ManualResetEvent を含む) が設定されている EventWaitHandle では、Set メソッドを呼び出すと、対応する Reset メソッドが呼び出されるまで待機ハンドルがシグナル状態のままになります。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からEventWaitHandle.Set メソッドを検索する場合は、下記のリンクをクリックしてください。

- EventWaitHandle.Set メソッドのページへのリンク