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

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.

主站蜘蛛池模板: 朝阳区| 措勤县| 府谷县| 双桥区| 天津市| 东至县| 兴宁市| 柞水县| 建平县| 建宁县| 修武县| 洛宁县| 鄱阳县| 云和县| 安溪县| 岚皋县| 邛崃市| 新化县| 邹城市| 进贤县| 辽宁省| 武乡县| 宜宾县| 年辖:市辖区| 乾安县| 定日县| 滕州市| 独山县| 莆田市| 高邑县| 闵行区| 庄河市| 涪陵区| 辛集市| 嘉黎县| 安徽省| 武山县| 长葛市| 易门县| 和林格尔县| 剑阁县|