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

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.

主站蜘蛛池模板: 阿巴嘎旗| 关岭| 高淳县| 马公市| 讷河市| 正定县| 庆元县| 盐山县| 溧阳市| 四子王旗| 秀山| 赫章县| 石嘴山市| 两当县| 文成县| 巢湖市| 临海市| 奉贤区| 雷山县| 乐平市| 凤台县| 宁远县| 万安县| 高州市| 陇南市| 彰化市| 牟定县| 府谷县| 昌平区| 蒙山县| 广西| 奎屯市| 新丰县| 搜索| 余干县| 乌兰察布市| 疏附县| 甘孜县| 四会市| 商丘市| 灌南县|