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

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.

主站蜘蛛池模板: 华坪县| 平乡县| 平山县| 富宁县| 万安县| 望江县| 留坝县| 洛宁县| 江都市| 嵊州市| 新乐市| 闸北区| 黔西| 吉木乃县| 高邮市| 安国市| 淳安县| 正安县| 鲁甸县| 泗水县| 龙泉市| 秭归县| 突泉县| 蛟河市| 英吉沙县| 玛沁县| 吉木萨尔县| 郸城县| 池州市| 鹿泉市| 溧阳市| 垦利县| 美姑县| 布拖县| 阳东县| 永安市| 上犹县| 南阳市| 永春县| 武宣县| 府谷县|