Wait Handles
This lesson discusses the abstract class WaitHandle and its derived classes in C#
We'll cover the following...
Wait Handles
In this section, we'll discuss the remaining derived classes of the abstract class WaitHandle
with examples.
EventWaitHandle
The EventWaitHandle
object can be either in a signaled or an unsignaled state. When signaled the object, depending on the reset mode we choose at the time of creating the object, will either automatically reset itself to an unsignaled state or need to be manually reset using the Reset()
method.
Press + to interact
using System;using System.Threading;class Demonstration{static void Main(){new EventWaitHandleExample().runTest();}}public class EventWaitHandleExample{EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset);EventWaitHandle done = new EventWaitHandle(false, EventResetMode.AutoReset);void work(int num){ewh.WaitOne();Console.WriteLine(num * num);done.Set();}public void runTest(){Thread t1 = new Thread(()=> {work(5);});Thread t2 = new Thread(() => {work(10);});t1.Start();t2.Start();Thread.Sleep(1000);WaitHandle.SignalAndWait(ewh, done);Console.WriteLine("Main thread exiting");t1.Join();t2.Join();}}
In the example above ...