Pergunta

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
    }
}
Foi útil?

Solução

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

Outras dicas

That's fine - it's kind of what it's meant to be used for!

See this MSDN article for more info. There's a section called 'Thread Safety' near the bottom which is listed for most classes on MSDN, so if you're unsure about whether any other classes are thread safe in the future, you can just check there.

The entire point of AutoResetEvent and ManualResetEvent is exactly to provide synchronisation between different threads!

see This article for details.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top