In C # multi-threaded programming, these two classes are almost indispensable. Their usage/Declaration are similar. So what is the difference?
Set Method sets the signal to the sending status Reset method sets the signal to the non-sending status WaitOne waiting for the signal to be sent
In fact, we can see from the name that the manual and automatic methods actually refer to the Reset method processing, as shown in the following example.
Public AutoResetEvent autoevent = new AutoResetEvent (true );
Public ManualResetEvent manualevent = new ManualResetEvent (true );
All signals are sent by default,
Autoevent. WaitOne ();
Manualevent. WaitOne ();
If a thread calls the above method, when the signal is in the sending status, the thread will receive a signal to continue execution.
After the difference is called, autoevent. WaitOne () allows only one thread to enter each time. When a thread gets a signal (that is, other threads call
Autoevent. after the Set () method), autoevent automatically sets the signal to not sent, and other threads that call WaitOne will only wait. that is to say, autoevent only wakes up one thread at a time
Manualevent can wake up multiple threads, because when a thread calls the set method, other threads that call waitone receive signals to continue execution, manualevent does not automatically set the signal to not send. that is, unless manualevent is manually called. reset (). method, then
Manualevent will remain in a signal state, and manualevent will be able to wake up multiple threads at the same time to continue execution