ManualResetEvent Class
public sealed class ManualResetEvent : WaitHandle
Signals one or more threads that an event has occurred. This class cannot be inherited.
Important Security Note:
The ManualResetEvent and ManualResetEventSlim classes are lightweight synchronization primitives. Be cautious when using them across multiple application domains. For more robust cross-domain synchronization, consider using Mutex or EventWaitHandle.
Inheritance
Object → WaitHandle → ManualResetEvent
Remarks
A ManualResetEvent is a synchronization primitive that can be used to synchronize access to resources. It is a thread-safe object that allows one or more threads to wait until the event is signaled. Once the event is signaled, all waiting threads are released.
The ManualResetEvent has two states: signaled and unsignaled. Initially, the event is unsignaled. Threads can wait on the event by calling one of its WaitOne() methods. The thread will block until the event is signaled.
To signal the event, call the Set() method. This releases all threads currently waiting on the event. Once signaled, the event remains signaled until Reset() is called. Calling Reset() sets the event back to the unsignaled state, and subsequent calls to WaitOne() will block again.
ManualResetEventSlim is a lighter-weight alternative for intra-process synchronization. It avoids the overhead of kernel handles when the event is signaled quickly. Use ManualResetEvent for inter-process synchronization or when ManualResetEventSlim's optimizations are not suitable.
Constructors
ManualResetEvent(bool initialState)
public ManualResetEvent(bool initialState)
Initializes a new instance of the ManualResetEvent class with the specified initial state.
initialStatetrueto set the signaled state to true; otherwise,false.
ManualResetEvent(bool initialState, string? name)
public ManualResetEvent(bool initialState, string? name)
Initializes a new instance of the ManualResetEvent class with the specified initial state and name.
initialStatetrueto set the signaled state to true; otherwise,false.name- The name of the event. If
null, the event is created anonymously.
Methods
Set()
public bool Set()
Sets the state of the event to signaled, allowing one or more waiting threads to proceed. This method does not return until the operation completes.
Returns: true if the operation succeeded; otherwise, false.
Reset()
public bool Reset()
Sets the state of the event to nonsignaled, causing threads to block.
Returns: true if the operation succeeded; otherwise, false.
WaitOne()
public bool WaitOne()
Waits indefinitely for the current thread to be released.
Returns: true.
WaitOne(int millisecondsTimeout)
public bool WaitOne(int millisecondsTimeout)
Waits one second for the current thread to be released.
millisecondsTimeout- The number of milliseconds to wait, or
-1to wait indefinitely.
Returns: true if the current thread was released before the timeout expired; otherwise, false.
WaitOne(int millisecondsTimeout, bool exitContext)
public bool WaitOne(int millisecondsTimeout, bool exitContext)
Waits for the current thread to be released, with an option to exit the synchronization domain first.
millisecondsTimeout- The number of milliseconds to wait, or
-1to wait indefinitely. exitContexttrueto exit the synchronization domain before blocking;falseto enter the synchronization domain after the method returns.
Returns: true if the current thread was released before the timeout expired; otherwise, false.
WaitOne(TimeSpan validityTime)
public bool WaitOne(TimeSpan validityTime)
Waits for the specified time for the current thread to be released.
validityTime- A
TimeSpan representing the number of milliseconds to wait, or aTimeSpan that represents a negative number of milliseconds to wait indefinitely.
Returns: true if the current thread was released before the timeout expired; otherwise, false.
WaitOne(TimeSpan validityTime, bool exitContext)
public bool WaitOne(TimeSpan validityTime, bool exitContext)
Waits for the specified time for the current thread to be released, with an option to exit the synchronization domain first.
validityTime- A
TimeSpan representing the number of milliseconds to wait, or aTimeSpan that represents a negative number of milliseconds to wait indefinitely. exitContexttrueto exit the synchronization domain before blocking;falseto enter the synchronization domain after the method returns.
Returns: true if the current thread was released before the timeout expired; otherwise, false.
Dispose()
public void Dispose()
Releases all resources used by the current instance of the
Dispose(bool disposing)
protected virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the
disposingtrueto release both managed and unmanaged resources;falseto release only unmanaged resources.
Example
using System;
using System.Threading;
public class Example
{
// A ManualResetEvent initialized to unsignaled state.
private static ManualResetEvent _mre = new ManualResetEvent(false);
public static void Main()
{
Console.WriteLine("Main thread starting.");
// Create a thread that waits for the event.
Thread workerThread = new Thread(Worker);
workerThread.Start();
Console.WriteLine("Main thread doing some work...");
Thread.Sleep(2000); // Simulate work
Console.WriteLine("Main thread signaling the event.");
_mre.Set(); // Signal the event
// Wait for the worker thread to finish (optional, for demonstration)
workerThread.Join();
Console.WriteLine("Main thread finished.");
}
public static void Worker()
{
Console.WriteLine("Worker thread: Waiting for the event...");
_mre.WaitOne(); // Wait until the event is signaled
Console.WriteLine("Worker thread: Event signaled. Resuming work.");
// Simulate more work after the event is signaled
Thread.Sleep(1000);
Console.WriteLine("Worker thread finished.");
}
}