Differences and examples
AutoResetEvent is very similar to ManualResetEvent. The difference between the two is that the former is automatic (Auto) and the latter is manual (Manua).
You can run the following example first and then test the difference between the two.
AutoResetEvent Example:
class Program { // Thread notification private static AutoResetEvent resetEvent = new AutoResetEvent(false); static void Main(string[] args) { // Create thread new Thread(DoOne).Start(); // Used to continuously send signals to another thread while (true) { (); (); // A notification occurs, setting a termination status } } public static void DoOne() { ("① Waiting, please send a signal to allow me to run"); (); ("② Waiting, please send a signal to allow me to run"); (); ("③ Waiting, please send a signal to allow me to run"); // ... ("Thread End"); } }
ManualResetEvent class example:
class Program { private static ManualResetEvent resetEvent = new ManualResetEvent(false); static void Main(string[] args) { new Thread(DoOne).Start(); // Used to continuously send signals to another thread while (true) { (); (); // A notification occurs, setting a termination status } } public static void DoOne() { ("Wait, please send a signal to allow me to run"); (); // All the following are invalid, the thread will skip directly without waiting (); (); (); (); (); ("Thread End"); } }
Because the AutoResetEvent object is in.WaitOne()
Method After waiting for the signal to be completed, it will automatically reset to the non-terminated state, which is equivalent to the automatic gate of the highway toll station. After a car passes, the machine will automatically close the gate.
ManualResetEvent is equivalent to a manual gate. After opening it, write to manually close the gate, otherwise the gate will remain open.
ManualResetEvent is mainly used for more flexible thread signaling scenarios.
ManualResetEvent class
It indicates a thread synchronization event. When a signal is received, if it wants to take effect next time, the event must be reset manually.
Because the ManualResetEvent class is very similar to the AutoManualResetEvent class, I won't go into details here.
Their usage differences are mainly:
AutoResetEvent class, every timeSet()
, skip oneWaitOne()
. BecauseAutomatically restore settings
, so next time I meetWaitOne()
Will continue to wait.
ManualResetEvent class,Set()
After that, it won'tReset settings
, so once usedSet()
After that, it will be opened all the way and will not wait any longer.
Its constructor is as follows:
Constructor | illustrate |
---|---|
ManualResetEvent(Boolean) | Initializes a new instance of the ManualResetEvent class with a boolean indicating whether to set the initial state to terminate. |
The commonly used methods are as follows:
method | illustrate |
---|---|
Close() | Frees all resources occupied by the current WaitHandle. |
Reset() | Set the event status to non-terminate, resulting in thread blockage. |
Set() | Set the event status to signal, allowing one or more waiting threads to continue execution. |
WaitOne() | Blocks the current thread until the current WaitHandle receives the signal. |
WaitOne(Int32) | Blocks the current thread until the current WaitHandle receives a signal while specifying the time interval in milliseconds using a 32-bit signed integer. |
WaitOne(Int32, Boolean) | Blocks the current thread until the current WaitHandle receives the signal, and uses a 32-bit signed integer to specify the time interval and specifies whether to exit the synchronization domain before waiting. |
WaitOne(TimeSpan) | Blocks the current thread until the current instance receives a signal, while specifying the time interval using TimeSpan. |
WaitOne(TimeSpan, Boolean) | Blocks the current thread until the current instance receives a signal, and uses TimeSpan to specify the time interval and specifies whether to exit the synchronization domain before waiting. |
ManualResetEventSlim
ManualResetEventSlim Compared to ManualResetEvent , this class can be used to obtain better performance than ManualResetEvent .
From the perspective of code usage, there is no difference, mainly considering the performance, the two scenarios are different.
I won't elaborate on these two types here.
This is all about this article about manual thread notifications in the C# multi-threading series. I hope it will be helpful to everyone's learning and I hope everyone will support me more.