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

Using the AutoResetEvent construct

In this recipe, there is an example of how to send notifications from one thread to another with the help of an AutoResetEvent construct. AutoResetEvent notifies a waiting thread that an event has occurred.

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

How to do it...

To understand how to send notifications from one thread to another with the help of the AutoResetEvent 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 snippet:
    private static AutoResetEvent _workerEvent = new AutoResetEvent(false);
    private static AutoResetEvent _mainEvent = new AutoResetEvent(false);
    
    static void Process(int seconds)
    {
      WriteLine("Starting a long running work...");
      Sleep(TimeSpan.FromSeconds(seconds));
      WriteLine("Work is done!");
      _workerEvent.Set();
      WriteLine("Waiting for a main thread to complete its work");
      _mainEvent.WaitOne();
      WriteLine("Starting second operation...");
      Sleep(TimeSpan.FromSeconds(seconds));
      WriteLine("Work is done!");
      _workerEvent.Set();
    }
  4. Inside the Main method, add the following code snippet:
    var t = new Thread(() => Process(10));
    t.Start();
    
    WriteLine("Waiting for another thread to complete work");
    _workerEvent.WaitOne();
    WriteLine("First operation is completed!");
    WriteLine("Performing an operation on a main thread");
    Sleep(TimeSpan.FromSeconds(5));
    _mainEvent.Set();
    WriteLine("Now running the second operation on a second thread");
    _workerEvent.WaitOne();
    WriteLine("Second operation is completed!");
  5. Run the program.

How it works...

When the main program starts, it defines two AutoResetEvent instances. One of them is for signaling from the second thread to the main thread, and the second one is for signaling from the main thread to the second thread. We provide false to the AutoResetEvent constructor, specifying the initial sate of both the instances as unsignaled. This means that any thread calling the WaitOne method of one of these objects will be blocked until we call the Set method. If we initialize the event state to true, it becomes signaled and the first thread calling WaitOne will proceed immediately. The event state then becomes unsignaled automatically, so we need to call the Set method once again to let the other threads calling the WaitOne method on this instance to continue.

Then, we create a second thread, which executes the first operation for 10 seconds and waits for the signal from the second thread. The signal notifies that the first operation is completed. Now, the second thread waits for a signal from the main thread. We do some additional work on the main thread and send a signal by calling the _mainEvent.Set method. Then, we wait for another signal from the second thread.

AutoResetEvent is a kernel-time construct, so if the wait time is not significant, it is better to use the next recipe with ManualResetEventslim, which is a hybrid construct.

主站蜘蛛池模板: 敦化市| 宜丰县| 特克斯县| 兴化市| 望江县| 定结县| 来安县| 太康县| 阳曲县| 兖州市| 江安县| 察哈| 霍林郭勒市| 大姚县| 依安县| 苏州市| 德钦县| 沁水县| 淳安县| 呼图壁县| 奈曼旗| 太仆寺旗| 天镇县| 栾城县| 明水县| 甘南县| 唐山市| 元阳县| 宝鸡市| 古丈县| 泗水县| 伊宁县| 三原县| 龙岩市| 长乐市| 秀山| 巧家县| 台州市| 伊宁县| 佛山市| 定日县|