- Multithreading with C# Cookbook(Second Edition)
- Eugene Agafonov
- 420字
- 2021-07-09 19:35:38
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:
- 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: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);
- 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();
- 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
.
- 計算機網絡
- 多媒體CAI課件設計與制作導論(第二版)
- 深入理解Bootstrap
- SpringMVC+MyBatis快速開發與項目實戰
- Python應用輕松入門
- Python:Master the Art of Design Patterns
- jQuery開發基礎教程
- 單片機應用與調試項目教程(C語言版)
- Java面向對象程序設計
- Image Processing with ImageJ
- Visual Basic程序設計(第三版)
- Java Web開發實例大全(基礎卷) (軟件工程師開發大系)
- Learning Ionic
- Offer來了:Java面試核心知識點精講(框架篇)
- 例說FPGA:可直接用于工程項目的第一手經驗