Multithreading with C# Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Using the CountDownEvent construct

This recipe will describe how to use the CountdownEvent signaling construct to wait until a certain number of operations complete.

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\Recipe6.

How to do it...

To understand the use of the CountDownEvent construct, perform the following steps:

  1. Start Visual Studio 2015. Create a new C# console application project.
  2. In the Program.cs file, add the following using directives:
    using System;
    using System.Threading;
    using static System.Console;
    using static System.Threading.Thread;
  3. Below the Main method, add the following code:
    static CountdownEvent _countdown = new CountdownEvent(2);
    
    static void PerformOperation(string message, int seconds)
    {
      Sleep(TimeSpan.FromSeconds(seconds));
      WriteLine(message);
      _countdown.Signal();
    }
  4. Inside the Main method, add the following code:
    WriteLine("Starting two operations");
    var t1 = new Thread(() => PerformOperation("Operation 1 is completed", 4));
    var t2 = new Thread(() => PerformOperation("Operation 2 is completed", 8));
    t1.Start();
    t2.Start();
    _countdown.Wait();
    WriteLine("Both operations have been completed.");
    _countdown.Dispose();
  5. Run the program.

How it works...

When the main program starts, we create a new CountdownEvent instance, specifying that we want it to signal when two operations complete in its constructor. Then, we start two threads that signal to the event when they are complete. As soon as the second thread is complete, the main thread returns from waiting on CountdownEvent and proceeds further. Using this construct, it is very convenient to wait for multiple asynchronous operations to complete.

However, there is a significant disadvantage; _countdown.Wait() will wait forever if we fail to call _countdown.Signal() the required number of times. Make sure that all your threads complete with the Signal method call when using CountdownEvent.