This recipe will describe how to use a CountdownEvent
signaling construct to wait until a certain number of operations complete.
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe could be found at BookSamples\Chapter2\Recipe6
.
To understand the use of the CountDownEvent
construct, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading;
Below the
Main
method, add the following code:static CountdownEvent _countdown = new CountdownEvent(2); static void PerformOperation(string message, int seconds) { Thread.Sleep(TimeSpan.FromSeconds(seconds)); Console.WriteLine(message); _countdown.Signal(); }
Inside the
Main
method, add the following code:Console.WriteLine("Starting two operations"); var t1 = new Thread(() =>...