Using the CountDownEvent construct
This recipe will describe how to use the CountdownEvent
signaling construct to wait until a certain number of operations complete.
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\Recipe6
.
How to do it...
To understand the use of the CountDownEvent
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 CountdownEvent _countdown = new CountdownEvent(2); static void PerformOperation(string message, int seconds) { Sleep(TimeSpan.FromSeconds(seconds)); WriteLine(message); _countdown.Signal(); }
Inside the
Main
method, add the following code:WriteLine("Starting...