System.Threading

AutoResetEvent Class

Signals a single thread that an event has occurred. This class cannot be inherited.

Remarks

The AutoResetEvent class is a synchronization primitive that allows one thread to signal another thread that an event has occurred. When a thread calls the Set method, the event is set to the signaled state, and one waiting thread is released. If no threads are waiting, the event remains in the signaled state until Set is called again.

When a thread calls the WaitOne method, it checks if the event is in the signaled state. If it is, the method returns true immediately, and the event is reset to the non-signaled state. If the event is not signaled, the thread blocks until the event is signaled by another thread calling Set. Once signaled, the waiting thread is released, and the event is automatically reset to the non-signaled state.

This automatic resetting behavior distinguishes AutoResetEvent from ManualResetEvent, which remains signaled until its Reset method is explicitly called.

Constructors

public AutoResetEvent(bool initialState);
Public Sub New(initialState As Boolean)

AutoResetEvent(bool initialState)

Initializes a new instance of the AutoResetEvent class, specifying whether the initial state is signaled.

initialState
true to set the initial state to signaled; otherwise, false.

Methods

public bool WaitOne();
public bool WaitOne(int millisecondsTimeout);
public bool WaitOne(TimeSpan timeout);
public bool WaitOne(int millisecondsTimeout, bool exitContext);
public bool WaitOne(TimeSpan timeout, bool exitContext);
Public Function WaitOne() As Boolean
Public Function WaitOne(millisecondsTimeout As Integer) As Boolean
Public Function WaitOne(timeout As TimeSpan) As Boolean
Public Function WaitOne(millisecondsTimeout As Integer, exitContext As Boolean) As Boolean
Public Function WaitOne(timeout As TimeSpan, exitContext As Boolean) As Boolean

WaitOne()

Blocks the current thread until the current AutoResetEvent is signaled.

Returns: true if the wait completed, or false if the wait timed out.

WaitOne(int millisecondsTimeout)

Blocks the current thread until the current AutoResetEvent is signaled or the specified time elapses.

millisecondsTimeout
The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely.

Returns: true if the wait completed, or false if the wait timed out.

WaitOne(TimeSpan timeout)

Blocks the current thread until the current AutoResetEvent is signaled or the specified time elapses.

timeout
A representing the number of milliseconds to wait, or a TimeSpan representing Timeout.InfiniteTimeSpan to wait indefinitely.

Returns: true if the wait completed, or false if the wait timed out.

WaitOne(int millisecondsTimeout, bool exitContext)

Blocks the current thread until the current AutoResetEvent is signaled or the specified time elapses, and optionally exits the synchronization domain before waiting.

millisecondsTimeout
The number of milliseconds to wait, or Timeout.Infinite (-1) to wait indefinitely.
exitContext
true to exit the synchronization domain before the wait begins; otherwise, false.

Returns: true if the wait completed, or false if the wait timed out.

WaitOne(TimeSpan timeout, bool exitContext)

Blocks the current thread until the current AutoResetEvent is signaled or the specified time elapses, and optionally exits the synchronization domain before waiting.

timeout
A representing the number of milliseconds to wait, or a TimeSpan representing Timeout.InfiniteTimeSpan to wait indefinitely.
exitContext
true to exit the synchronization domain before the wait begins; otherwise, false.

Returns: true if the wait completed, or false if the wait timed out.

public bool Set();
Public Function Set() As Boolean

Set()

Sets the AutoResetEvent to the signaled state, releasing one waiting thread. If no threads are waiting, the event remains signaled until a thread calls WaitOne.

Returns: true if the method succeeded; otherwise, false.

Properties

public sealed override WaitHandle Handle { get; }
Public Overrides ReadOnly Property Handle As WaitHandle

Handle

Gets a handle to the operating system synchronization primitive.

Property Value: A handle to the underlying system synchronization primitive.

Example

The following example demonstrates how to use AutoResetEvent to synchronize access to a shared resource between two threads.

using System;
using System.Threading;

public class AutoResetEventExample
{
    private static AutoResetEvent _autoResetEvent = new AutoResetEvent(false); // Initially not signaled
    private static int _sharedResource = 0;

    public static void Main(string[] args)
    {
        Console.WriteLine("Starting AutoResetEvent example...");

        // Create a producer thread
        Thread producerThread = new Thread(Produce);
        producerThread.Start();

        // Create a consumer thread
        Thread consumerThread = new Thread(Consume);
        consumerThread.Start();

        Console.WriteLine("Main thread waiting for threads to finish...");
        producerThread.Join();
        consumerThread.Join();

        Console.WriteLine("AutoResetEvent example finished.");
    }

    public static void Produce()
    {
        Console.WriteLine("Producer: Generating data...");
        Thread.Sleep(1000); // Simulate work
        _sharedResource = 42;
        Console.WriteLine("Producer: Data generated. Signaling consumer.");
        _autoResetEvent.Set(); // Signal the consumer
    }

    public static void Consume()
    {
        Console.WriteLine("Consumer: Waiting for data...");
        _autoResetEvent.WaitOne(); // Wait for the producer to signal
        Console.WriteLine($"Consumer: Received data: {_sharedResource}");
    }
}