In this recipe, there is an example of how to send notifications from one thread to another with the help of an AutoResetEvent construct. AutoResetEvent notifies a waiting thread that an event has occurred.
Getting ready
To step through this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter2\Recipe4.
How to do it...
To understand how to send notifications from one thread to another with the help of the AutoResetEvent construct, perform the following steps:
Start Visual Studio 2015. Create a new C# console application project.
In the Program.cs file, add the following using directives:
using System;
using System.Threading;
using static System.Console;
using static System.Threading.Thread;
Below the Main method, add the following code snippet:
private static AutoResetEvent _workerEvent = new AutoResetEvent(false);
private static AutoResetEvent _mainEvent = new AutoResetEvent(false);
static void Process(int seconds)
{
WriteLine("Starting a long running work...");
Sleep(TimeSpan.FromSeconds(seconds));
WriteLine("Work is done!");
_workerEvent.Set();
WriteLine("Waiting for a main thread to complete its work");
_mainEvent.WaitOne();
WriteLine("Starting second operation...");
Sleep(TimeSpan.FromSeconds(seconds));
WriteLine("Work is done!");
_workerEvent.Set();
}
Inside the Main method, add the following code snippet:
var t = new Thread(() => Process(10));
t.Start();
WriteLine("Waiting for another thread to complete work");
_workerEvent.WaitOne();
WriteLine("First operation is completed!");
WriteLine("Performing an operation on a main thread");
Sleep(TimeSpan.FromSeconds(5));
_mainEvent.Set();
WriteLine("Now running the second operation on a second thread");
_workerEvent.WaitOne();
WriteLine("Second operation is completed!");
Run the program.
How it works...
When the main program starts, it defines two AutoResetEvent instances. One of them is for signaling from the second thread to the main thread, and the second one is for signaling from the main thread to the second thread. We provide false to the AutoResetEvent constructor, specifying the initial sate of both the instances as unsignaled. This means that any thread calling the WaitOne method of one of these objects will be blocked until we call the Set method. If we initialize the event state to true, it becomes signaled and the first thread calling WaitOne will proceed immediately. The event state then becomes unsignaled automatically, so we need to call the Set method once again to let the other threads calling the WaitOne method on this instance to continue.
Then, we create a second thread, which executes the first operation for 10 seconds and waits for the signal from the second thread. The signal notifies that the first operation is completed. Now, the second thread waits for a signal from the main thread. We do some additional work on the main thread and send a signal by calling the _mainEvent.Set method. Then, we wait for another signal from the second thread.
AutoResetEvent is a kernel-time construct, so if the wait time is not significant, it is better to use the next recipe with ManualResetEventslim, which is a hybrid construct.