EventWaitHandle.Set Method ()

 

Sets the state of the event to signaled, allowing one or more waiting threads to proceed.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

member Set : unit -> bool

Return Value

Type: System.Boolean

true if the operation succeeds; otherwise, false.

Exception Condition
ObjectDisposedException

The Close method was previously called on this System.Threading.EventWaitHandle.

For an EventWaitHandle with EventResetMode.AutoReset (including AutoResetEvent), the Set method releases a single thread. If there are no waiting threads, the wait handle remains signaled until a thread attempts to wait on it, or until its Reset method is called.

System_CAPS_importantImportant

There is no guarantee that every call to the Set method will release a thread from an EventWaitHandle whose reset mode is EventResetMode.AutoReset. If two calls are too close together, so that the second call occurs before a thread has been released, only one thread is released. It is as if the second call did not happen. Also, if Set is called when there are no threads waiting and the EventWaitHandle is already signaled, the call has no effect.

For an EventWaitHandle with EventResetMode.ManualReset (including ManualResetEvent), calling the Set method leaves the wait handle in a signaled state until its Reset method is called.

The following code example uses the SignalAndWait(WaitHandle, WaitHandle) method overload to allow the main thread to signal a blocked thread and then wait until the thread finishes a task.

The example starts five threads and allows them to block on an EventWaitHandle created with the EventResetMode.AutoReset flag, then releases one thread each time the user presses the ENTER key. The example then queues another five threads and releases them all using an EventWaitHandle created with the EventResetMode.ManualReset flag.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: