In this post, we will be discussing how we can use EventWaitHandle to signal between different windows. We will also see how the same logic applies to signal between different processes. We will find how we can consume these notifications using regular threads and ThreadPool’s WaitHandle registration features. In the later part, we will be touching on CountdownEvent which is a new feature in .net 4.0.

Let us create a sample application. It has two kind of windows. One window would serve as Server Window. It has a responsibility of generating notifications for different parts in the same window or across different windows in the same or different processes. It has three buttons and a textual notification. Signal button would give notification to the interesting thread created by the same window. Open another window opens a new window for consumption of signals. Signal another window generates notifications for the other window. The XAML for the above window is as follows:

The code behind for this is as follows:

In the above code, we are creating an AutoResetEvent, named _waitHandle. In the constructor, we are waiting for this EventWaitHandle in two different threads by calling WaitOne on this EventWaitHandle. As soon as the thread is notified, we are setting the content of LabelStatus as Signal received1 for first thread and Signal received2 for second thread. When the constructor ends, two threads are ready to receive notification. As soon as they would receive the signal, they can resume their operation. Since this is an AutoResetEvent, each thread would wait for its turn. A signal is received by only one. This one is generally the one at the top of the queue.

Now run the application. Click on Signal button. The first thread in the queue gets the signal. This thread now can resume its operation and updates the Label Status as Signal received1. Now hit the Signal button again. Now second thread gets the notification and updates the LabelStatus as Signal received2. You might have noticed (in the event handler for Click event for button1, button1_Click), we are showing OtherWindow. The OtherWindow is defined as follows:

The code behind for this window is as follows:

In this window _waitHandle is the similar EventWaitHandle as in MainWindow. This is a ManualReset type of EventWaitHandle. The name of event is “MyWaitHandle”. Like MainWindow’s, here we are starting a new thread which is waiting for this EventWaitHandle. It must be remembered that as soon as a Signal is received for ManualReset EventWaitHandle, all threads waiting for the signal gets this notification and they resume their operation. After signal has been generated, any thread which is waiting or will be waiting in future finds this open gate and just continue running unless the EventWaitHandle resets. In order to test this, lets click Open another window button three times showing three instances of AnotherWindow. As we click the button “Signal another window”, the following code executes:

which is nothing but signaling the waiting threads on this event to resume operation (ManualReset). The threads in all the Window updates their respective Label contents by following code:

The windows are updates as follows: Basically, the following statement creates a System level EventWaitHandle:

We have to make sure that the name of this event is unique so that other applications and processes are not affected by these signals. Here we have defined a ManualRestEvent, we could also define an AutoReset EventWaitHandle. Then clicking button each time a different window would receive the signal and update the content of its Label. In order to prove the point of System level EventWaitHandle, let us create a new WPF project, WpfApplicationB_Signaling. This has just one window, MainWindow. The Window is defined as follows:

The code behind of the Window is as follows:

Here we are using ThreadPool functionality of handling EventWaitHandle. We are registering EventWaitHandle using RegisterWaitForSingleObject method of ThreadPool. For this method, we need to specify which Event to wait on (_waitHandleNamed). We also specify if there is a Timeout. Since we can wait forever, we are specifying Timeout as Timeout.Infinite. The last argument is specified as true. This is to specify that we are interested in just one signal, otherwise, after finishing executing the handler, it would continue to wait for more notifications.

Now run this along with the previous WPF project. As you can see that we have opened three instances of AnotherWindow from 1st process. We also have MainWindow of second process opened: Now click “Signal another window” button. All three AnotherWindow(s) and MainWindow from 2nd process updates as follows: It is just like water flow. All taps which are opened, should get water from the flow: Resume, only when all operations are completed!

In the above examples, we have considered the examples of AutoReset and ManualRest EventWaitHandles. .Net 4.0 has introduce a new signaling mechanism between threads. This is called CountdownEvent. Though this is named like the other two and it is also used in signaling between threads, it does not inherit EventWaitHandle unlike other two.

In this example, we can only notify the user when three operations complete. In order to keep the example simple, we are using three buttons. The click of each button would be considered as completion of an operation. When all three operation completes, the textblock should notify the user with text Process Finshed!!!. Just click each button only once :) The definition of the above window is as follows:

The code behind is as follows:

The above code is very simple. We have created an instance of CountdownEvent with the required signal count as 3. In the constructor, we have created a thread waiting for the CountdownEvent to finish. After each button click we are generating a signal. When the signal count reaches 3, the thread can resume. Now run the application and click each button only once. As soon as we click the third button, the window is updates as follows: Note: Since CountdownEvent does not inherit EventWaitHandle, there is no named CountdownEvent available in .net 4.0. Had there been any named event available for this type, we might implement many scenarios efficiently just by using CountdownEvent. This seems to be a big limitation.
[null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGJ1SrhaHI/AAAAAAAAAjE/oXsqmikmNJs/s400/1.jpg", "src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGJ1SrhaHI/AAAAAAAAAjE/oXsqmikmNJs/s400/1.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://1.bp.blogspot.com/_fVxKMZVpfzU/TRGN3y16mPI/AAAAAAAAAjM/5GSZk6TFKXI/s400/2.jpg", "src": "http://1.bp.blogspot.com/_fVxKMZVpfzU/TRGN3y16mPI/AAAAAAAAAjM/5GSZk6TFKXI/s400/2.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGOb8H5LVI/AAAAAAAAAjU/muuQDnoMmgk/s400/3.jpg", "src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGOb8H5LVI/AAAAAAAAAjU/muuQDnoMmgk/s400/3.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGPAWfH6WI/AAAAAAAAAjc/PBK3tylpawY/s400/4.jpg", "src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGPAWfH6WI/AAAAAAAAAjc/PBK3tylpawY/s400/4.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGQa-vQ3vI/AAAAAAAAAjk/3TJxovYICQo/s400/5.jpg", "src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGQa-vQ3vI/AAAAAAAAAjk/3TJxovYICQo/s400/5.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGRDf4mK2I/AAAAAAAAAjs/yQoXljHorQI/s400/6.jpg", "src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGRDf4mK2I/AAAAAAAAAjs/yQoXljHorQI/s400/6.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://1.bp.blogspot.com/_fVxKMZVpfzU/TRGRzSA8jXI/AAAAAAAAAj0/dauZlG9Y8Kc/s400/7.jpg", "src": "http://1.bp.blogspot.com/_fVxKMZVpfzU/TRGRzSA8jXI/AAAAAAAAAj0/dauZlG9Y8Kc/s400/7.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGUjNEqtmI/AAAAAAAAAj8/rsjn5ZcPUZM/s400/8.jpg", "src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGUjNEqtmI/AAAAAAAAAj8/rsjn5ZcPUZM/s400/8.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGU9O-QooI/AAAAAAAAAkE/skPGf1lMrH8/s400/9.jpg", "src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TRGU9O-QooI/AAAAAAAAAkE/skPGf1lMrH8/s400/9.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGVQWBu90I/AAAAAAAAAkM/fLrgvD2gP_A/s400/10.jpg", "src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TRGVQWBu90I/AAAAAAAAAkM/fLrgvD2gP_A/s400/10.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGWDpPoVtI/AAAAAAAAAkU/OI6OjY8IiGE/s400/11.jpg", "src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGWDpPoVtI/AAAAAAAAAkU/OI6OjY8IiGE/s400/11.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGYckpwM3I/AAAAAAAAAkc/5J_l2B0tGmE/s400/12.jpg", "src": "http://2.bp.blogspot.com/_fVxKMZVpfzU/TRGYckpwM3I/AAAAAAAAAkc/5J_l2B0tGmE/s400/12.jpg"}, null, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://3.bp.blogspot.com/-StVialkfDd8/UCGhpO3j02I/AAAAAAAABuM/_XHqvYMcULs/s195/MVVMSuvivalGuideCover.png", "src": "http://3.bp.blogspot.com/-StVialkfDd8/UCGhpO3j02I/AAAAAAAABuM/_XHqvYMcULs/s195/MVVMSuvivalGuideCover.png", "formatted_filename": "MVVMSuvivalGuideCover", "rendered_width": 161, "rendered_height": 195}, null, null, null, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-notification-through-thread.html", "unformatted_src": "http://1.bp.blogspot.com/-AM-f-uCY_uU/UbDSwj9c0JI/AAAAAAAAF1Q/0iXTkrmoBZU/s195/VSM.png", "src": "http://1.bp.blogspot.com/-AM-f-uCY_uU/UbDSwj9c0JI/AAAAAAAAF1Q/0iXTkrmoBZU/s195/VSM.png", "formatted_filename": "VSM", "rendered_width": 195, "rendered_height": 54}, null, null, null, null, null]