SoFunction
Updated on 2025-04-15

Summary of the usage of EventWaitHandle in C#

EventWaitHandleis a class in C# for inter-thread synchronization. It provides access control of shared resources and a synchronization mechanism between threads.EventWaitHandleClass is locatedUnder the namespace, it is mainly used to implement mutually exclusive access, semaphore control and other scenarios.

Create EventWaitHandle

Create aEventWaitHandleTwo parameters need to be specified: initial state and reset mode.

using ;

// Create an automatic reset event with the initial state not setEventWaitHandle autoEvent = new EventWaitHandle(false, );

// Create an automatic reset event with the initial state setEventWaitHandle manualEvent = new EventWaitHandle(true, );

Use EventWaitHandle

Setting and resetting events

  • Set(): When the event is set, all threads waiting for the event will be released.
  • Reset(): Reset the status of the event to the unset state.

ForAutoResetType ofEventWaitHandle, once the set state is triggered, it will immediately reset back to the unset state. andManualResetTypes need to be called explicitlyReset()Method to reset the event.

();
Reset the status of the manualEvent to not set (i.e. the semaphore is false). This usually means that any thread waiting for the event will continue to wait.
This is necessary for EventWaitHandle of type ManualReset; otherwise, even if the Set() method has been called, the waiting thread will not be awakened.
();
Set the status of the manualEvent to set (i.e., the semaphore is true). If any threads are waiting for this event, they will no longer wait and continue to execute.
For EventWaitHandle of type ManualReset, the status needs to be reset manually before waiting again.

(); // Set events manually(); // Manual reset event

Waiting for events

  • WaitOne(): Make the calling thread enter a waiting state until the event is set or timed out (if a timeout time is specified).
(); // Indefinitely waiting for the event to be setbool result = (5000); // exist5Waiting for events to be set within seconds

Written as WaitOne(-1) means that the current thread will block until the manualEvent is set (i.e., the semaphore becomes true).
-1 means waiting indefinitely until the event is set. If you want to specify a waiting timeout time, you can use the millisecond value as a parameter.

Example

Here is a simple example to showEventWaitHandleBasic usage of  :

using System;
using ;

class Program
{
    static void Main()
    {
        EventWaitHandle eventWaitHandle = new EventWaitHandle(false, );

        Thread thread = new Thread(() =>
        {
            ("Thread is waiting for the signal.");
            (); // Thread waits for signal            ("Thread received the signal and continues execution.");
        });

        ();

        (2000); // The main thread waits for two seconds before sending a signal        ("Main thread signals the waiting thread.");

        (); // Send signal        (); // Close event handle    }
}

In this example, we created a manual reset typeEventWaitHandleAnd wait for the signal in the thread. The main thread waits for two seconds before sending a signal to the waiting thread. When the signal is sent, the waiting thread continues to execute. Note that after use, it should be closedEventWaitHandle

Things to note

  • EventWaitHandlecan be named, which means it can be used across processes, but this is not shown in the example.
  • If namedEventWaitHandle, please make sure to handle mutex and permission issues correctly.
  • After usingEventWaitHandleAfter that, it should be calledClose()Method to release resources.

This is the end of this article about the usage of EventWaitHandle in C#. For more related C# EventWaitHandle content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!