AutoResetEvent and ManualResetEvent are used in threading and help us to manage synchronization using signals.
For example, suppose there are 2 threads, Thread1 and Thread2 and 1 main thread created by the Main() method.
Thread1 and Thread2 is doing some task and the Main thread is also doing some task (perhaps printing numbers). Now, we want to implement synchronization between these threads.
Let's play with an example.
First, we'll look at AutoResetEvent and then ManualResetEvent.
We'll understand both the concepts using their outputs, to understand the execution flow between these threads, our program is implemented in a colorful way. The following table shows the colors and their use in the program.
Color |
Used for |
Cyan |
Method Title (in other words AutoReset / ManualReset) |
Yellow |
Used for Thread1 and Thread2 |
Green |
Used for Main Thread |
Magenta |
Displays name of the Current Running Thread |
Create a new Console application named “AutoResetEventAndManulResetEvent” with the 2 Methods AutoResetMethod() and ManualResetEvent().
Program.cs
In the preceding example, 2 Threads are created, in other words Thread1 and Thread2.
AutoResetMethod() is running on Thread1 and ManualResetMethod() is running on Thread2 and the others are running on the main thread.
the same logic is used in both methods.
To use AutoResetEvent and ManualResetEvent in your program, create an object of AutoResetEvent and ManualResetEvent, that provides 2 methods for pausing and starting any thread as shown in the following table.
Method |
Description |
WaitOne() |
Block the current thread until WaitHandler receives a signal |
Set() |
Allow one or more waiting threads to proceed. |
Always remember that, for every WaitOne() we should have a Set() that actually releases the paused thread.
So, first we'll see the functionality of AutoResetEvent, (while running AutoResetEvent, please comment t2 Thread and ManualResetEvent objects and vice-a-versa).
Thread1 points to AutoResetMethod() and Thread2 points to ManualResetMethod() that contains some code that performs 2 Tasks.
The main thread's task is to print some numbers.
Run the program. (While running AutoResetEvent please comment “manualReset.WaitOne();”and while running ManualResetEvent please comment “autoReset.WaitOne();”at both the places in Main()).
Output (AutoResetEvent):
So, when you run the program in AutoResetEvent mode, Thread1 will start Task1 (in Yellow color) and it'll ask you to press ENTER to pause that thread (using WaitOne()).
Once ENTER is pressed, Thread1 will pause Task1 and control will directly shift to Main Thread where Mai Thread will perform its task, in other words to print numbers (in Green Color). As soon as Main Thread completes its task, it'll send a signal to Thread1 (using Set()) that was paused and Thread1 will start its execution again for Task1.
After completion of Task1, Thread1 will start Task2 and again it'll ask you to press ENTER to pause the thread, again control will shift to the main thread to perform its task and after completion of task it'll return back to Thread1. Thread1 will complete the remaining task and after completion closes the program.
Output (ManualResetEvent):
When you run the program in ManualResetEvent mode, Thread2 will start Task1 (in Yellow color) and it'll ask you to press ENTER to pause that thread (using WaitOne()).
After pressing ENTER, Thread2 will pause Task1 and control will directly shift to Main Thread where Main Thread will perform its task, in other words print numbers (in Green Color).
Now watch here carefully, as soon as the main thread completes its task, of printing numbers, using the first Set() method it'll send a signal to Thread2 to begin its execution. Thread2 will begin its execution and after completion of Task1, Thread2 will start Task2 and it'll again ask you to press ENTER to pause Thread2, but as soon as control reaches the second WaitOne() the control will not be shifted to the main thread, instead the program will continue its execution and will start and complete Task2 as well. Now control will leave ManualResetEvent().
Now, press ENTER, because the control is in Main(), after Pressing ENTER the main thread will complete its task and your program will close.
This happened because, in the case of ManualResetEvent, 1 Set() method will revokes all the WaitOne() methods and this is the main difference between AutoResetEvent and ManualResetEvent.
Conclusion
When we use AutoResetEvent, for every WaitOne(), we must call Set(). But when we use ManualResetEvent() 1 Set() will revoke all the WaitOne() in our program.
I hope this article helps you to understand the difference between AutoResetEvent and ManualResetEvent.
If there's any mistake in this article please let me know. Please provide your valuable feedback and comments, that enable me to provide a better article the next time.