Search code examples
c#multithreadingautoresetevent

Is it safe to call methods AutoResetEvent from different threads?


Is it safe to call methods AutoResetEvent from different threads? Or should I avoid calls of the object from different threads?

class Test 
{
    EventWaitHandle wh = new AutoResetEvent(false);
    Thread worker = new(Work);

    public Test
    {
        worker.Start();
    }

    public void StopSignal()
    {
        wh.Set(); //<-- Main thread Call
    }

    void Work() 
    {
        wh.WaitOne(); //<-- Child thread call
    }
}

Solution

  • Yes, it safe to call methods AutoResetEvent from different threads. Have a look at this link. I hope this makes you clear.