官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 东辽县| 万源市| 临高县| 台湾省| 山阳县| 德江县| 永和县| 那坡县| 抚顺市| 蒙城县| 安阳市| 教育| 林州市| 普安县| 新巴尔虎左旗| 怀柔区| 咸丰县| 调兵山市| 曲水县| 晋州市| 方正县| 蕲春县| 江川县| 怀柔区| 洪江市| 黄陵县| 长白| 宽城| 讷河市| 集安市| 柳河县| 新乐市| 尉犁县| 温泉县| 修文县| 锦屏县| 靖安县| 松溪县| 姜堰市| 青州市| 丰都县|