using System.Threading; //Create an EventWaitHandle with an initial state of false EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset); //Thread 1 will wait for event to be set Thread thread1 = new Thread(() => { Console.WriteLine("Thread 1 waiting for event to be set..."); ewh.WaitOne(); Console.WriteLine("Thread 1 finished!"); }); //Thread 2 will set the event Thread thread2 = new Thread(() => { Console.WriteLine("Thread 2 setting event..."); ewh.Set(); }); thread1.Start(); thread2.Start();
using System.Threading; //Create an EventWaitHandle with an initial state of true EventWaitHandle ewh = new EventWaitHandle(true, EventResetMode.ManualReset); //Thread 1 will wait for event to be set Thread thread1 = new Thread(() => { Console.WriteLine("Thread 1 waiting for event to be set..."); ewh.Reset(); ewh.WaitOne(); Console.WriteLine("Thread 1 finished!"); }); //Thread 2 will set the event Thread thread2 = new Thread(() => { Console.WriteLine("Thread 2 setting event..."); ewh.Set(); }); thread1.Start(); thread2.Start();In this example, we create an EventWaitHandle with an initial state of true and two threads. Thread 1 will reset the event using the Reset method, which will cause it to block until the event is set again. Thread 2 will set the event using the Set method. Once the event is set again, Thread 1 will unblock and print "Thread 1 finished!" to the console. Both examples are using the System.Threading package library.