سؤال

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
    }
}
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top