CountdownEvent cde = new CountdownEvent(3); ThreadPool.QueueUserWorkItem(state => { Console.WriteLine("Operation 1 complete"); cde.Signal(); }); ThreadPool.QueueUserWorkItem(state => { Console.WriteLine("Operation 2 complete"); cde.Signal(); }); ThreadPool.QueueUserWorkItem(state => { Console.WriteLine("Operation 3 complete"); cde.Signal(); }); cde.Wait(); // Blocks until all signals have been received cde.Dispose(); // Cleanup the CountdownEvent object
using (CountdownEvent cde = new CountdownEvent(1)) { ThreadPool.QueueUserWorkItem(state => { Console.WriteLine("Operation complete"); // Decrement the count of the CountdownEvent object cde.Signal(); }); // Blocks until all signals have been received or Dispose is called cde.Wait(); }In this example, we are using the CountdownEvent class to synchronize a single operation. We are creating a CountdownEvent object with a count of 1. We then use ThreadPool.QueueUserWorkItem to execute the operation on a separate thread. After the operation completes, we call the cde.Signal method to decrement the count of the CountdownEvent object. Finally, we call the cde.Wait method to wait until the signal has been received or the CountdownEvent object is disposed with the using statement.