vote buttons
0
1
beginner
0
intermediate
0
advanced
09-Nov-2014 05:17 UTC
Charlie
82

1 Answers

vote buttons
0

CountdownEvent is a synchronization primitive that unblocks its waiting threads after it has been signaled a certain number of times. E.g. if we create a CountdownEvent with a parameter of 3, threads waiting on it will remain blocked until a Signal event is called 3 times.


static CountdownEvent countdownEvent= new CountdownEvent (3);
static void Main()
{
  new Thread (Func).Start (1);
  new Thread (Func).Start (2);
  new Thread (Func).Start (3);
 
  countdownEvent.Wait();   // The thread will block until we receieve three Signals.
  Console.WriteLine("Three Signals received. Now I can proceed");
}
 
static void Func (object id)
{
  Thread.Sleep((int)id * 1000);
  Console.WriteLine("Signalling");
  countdownEvent.Signal();
}

Notes:

CountdownEvent is available only in .net framework 4.0 and above
It is purely managed. So cannot be used for interprocess signalling.
CountdownEvent’s count can be increased by calling the AddCount function (beware if it has already reached zero, AddCount will throw an exception)
Calling Reset() will reset its count to the original value and will unsignal the construct.

09-Nov-2014 05:18 UTC
Charlie
82
Related Questions