- Multithreading with C# Cookbook(Second Edition)
- Eugene Agafonov
- 468字
- 2021-07-09 19:35:38
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:
- Start Visual Studio 2015. Create a new C# console application project.
- In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading; using static System.Console; using static System.Threading.Thread;
- 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(); }
- 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!");
- 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.
- C++面向?qū)ο蟪绦蛟O計(第三版)
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- SoapUI Cookbook
- What's New in TensorFlow 2.0
- Python數(shù)據(jù)分析基礎
- 算法精粹:經(jīng)典計算機科學問題的Java實現(xiàn)
- JavaScript+jQuery開發(fā)實戰(zhàn)
- Oracle數(shù)據(jù)庫從入門到運維實戰(zhàn)
- Big Data Analytics
- 飛槳PaddlePaddle深度學習實戰(zhàn)
- Getting Started with NativeScript
- Raspberry Pi Home Automation with Arduino(Second Edition)
- C語言程序設計實訓教程與水平考試指導
- Android Sensor Programming By Example
- Java程序設計教程