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

Using the ManualResetEventSlim construct

This recipe will describe how to make signaling between threads more flexible with the ManualResetEventSlim construct.

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

How to do it...

To understand the use of the ManualResetEventSlim 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 void TravelThroughGates(string threadName, int seconds)
    {
      WriteLine($"{threadName} falls to sleep");
      Sleep(TimeSpan.FromSeconds(seconds));
      WriteLine($"{threadName} waits for the gates to open!");
      _mainEvent.Wait();
      WriteLine($"{threadName} enters the gates!");
    }
    
    static ManualResetEventSlim _mainEvent = new ManualResetEventSlim(false);
  4. Inside the Main method, add the following code:
    var t1 = new Thread(() => TravelThroughGates("Thread 1", 5));
    var t2 = new Thread(() => TravelThroughGates("Thread 2", 6));
    var t3 = new Thread(() => TravelThroughGates("Thread 3", 12));
    t1.Start();
    t2.Start();
    t3.Start();
    Sleep(TimeSpan.FromSeconds(6));
    WriteLine("The gates are now open!");
    _mainEvent.Set();
    Sleep(TimeSpan.FromSeconds(2));
    _mainEvent.Reset();
    WriteLine("The gates have been closed!");
    Sleep(TimeSpan.FromSeconds(10));
    WriteLine("The gates are now open for the second time!");
    _mainEvent.Set();
    Sleep(TimeSpan.FromSeconds(2));
    WriteLine("The gates have been closed!");
    _mainEvent.Reset();
  5. Run the program.

How it works...

When the main program starts, it first creates an instance of the ManualResetEventSlim construct. Then, we start three threads that wait for this event to signal them to continue the execution.

The whole process of working with this construct is like letting people pass through a gate. The AutoResetEvent event that we looked at in the previous recipe works like a turnstile, allowing only one person to pass at a time. ManualResetEventSlim, which is a hybrid version of ManualResetEvent, stays open until we manually call the Reset method. Going back to the code, when we call _mainEvent.Set, we open it and allow the threads that are ready to accept this signal to continue working. However, thread number three is still sleeping and does not make it in time. We call _mainEvent.Reset and we thus close it. The last thread is now ready to go on, but it has to wait for the next signal, which will happen a few seconds later.

There's more…

As in one of the previous recipes, we use a hybrid construct that lacks the possibility to work at the operating system level. If we need to have a global event, we should use the EventWaitHandle construct, which is the base class for AutoResetEvent and ManualResetEvent.

主站蜘蛛池模板: 静乐县| 曲松县| 达州市| 澄江县| 融水| 汤阴县| 营山县| 五家渠市| 宁安市| 张家港市| 恩平市| 保靖县| 灌阳县| 定州市| 镇原县| 莱芜市| 绩溪县| 手机| 博白县| 浦江县| 洛浦县| 开江县| 旬邑县| 宝应县| 博白县| 孟津县| 博白县| 略阳县| 乐安县| 应城市| 新泰市| 无棣县| 平远县| 于都县| 阳信县| 中宁县| 沙田区| 绥江县| 大连市| 塔城市| 灌南县|