System.Threading.CountdownEvent is a synchronization primitive that, after receiving a certain number of signals, will unlock its waiting thread. Countdownevent has an initial count at initialization, which is called when each work item is completedSignal. each time Signal is called, the signal count decrements by 1. On the main thread, the call to Wait will block until the signal count is zero.
1 Static voidMain (string[] args)2 {3Countdownevent countdown =NewCountdownevent (3);4 intTaskID =1;5 for(vari =0; I <3; i++)6 {7Task.Factory.StartNew (() =8 {9 intCur = taskid++;TenConsole.WriteLine ($"Task[{cur}] is Running"); OneThread.Sleep (TaskID * +); A Countdown. Signal (); -Console.WriteLine ($"Task[{cur}] Exit"); - }); the } -Console.WriteLine ($"Main Wait"); - Countdown. Wait (); -Console.WriteLine ($"Main Exit"); + Console.readkey (); -}
countdownevent Sample Code
Countdownevent is similar to barrier, but it is more convenient to use barrier in multiple waits.
[. NET multithreading] Countdownevent