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

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.

主站蜘蛛池模板: 阳春市| 龙江县| 承德市| 错那县| 长兴县| 类乌齐县| 南郑县| 麻阳| 盐城市| 沙坪坝区| 兴国县| 岳阳市| 海南省| 阳新县| 济阳县| 石家庄市| 东丽区| 西宁市| 天柱县| 吉林省| 循化| 宁阳县| 韶山市| 塔河县| 山丹县| 绵竹市| 苏尼特右旗| 呼伦贝尔市| 东海县| 油尖旺区| 芦山县| 墨江| 临海市| 青神县| 凌源市| 依兰县| 贵定县| 彰化县| 会昌县| 富宁县| 尉犁县|