C# 基于特定数量同步的机制CountdownEvent
1 static void Main(string[] args) 2 { 3 CountdownEvent countdownEvent = new CountdownEvent(4); 4 for (int i = 0; i < 4; i++) 5 { 6 int index = i; 7 Task.Run(async () => { 8 9 await Task.Delay(1000); 10 countdownEvent.Signal(); 11 Console.WriteLine(index); 12 }); 13 } 14 countdownEvent.Wait();//4个信号全部完成后,继续往下执行 15 Console.WriteLine("主线程完成"); 16 countdownEvent.Dispose(); 17 Console.ReadKey(); 18 }
posted on 2021-11-16 16:00 Shine-Zhong 阅读(44) 评论(0) 编辑 收藏 举报