Maison > Article > développement back-end > Utilisation de CountDownEvent en c#
L'utilisation de CountDownEvent en c#
class Program { static CountdownEvent _count = new CountdownEvent(3); static void Main(string[] args) { Task.Factory.StartNew(() => { Thread.Sleep(2000); Console.WriteLine("thread 1 complete"); _count.Signal(); }); Task.Factory.StartNew(() => { Thread.Sleep(5000); Console.WriteLine("thread 2 complete"); _count.Signal(); }); Task.Factory.StartNew(() => { Thread.Sleep(3000); Console.WriteLine("thread 3 complete"); _count.Signal(); }); Console.WriteLine("waiting tasks...."); _count.Wait(); Console.WriteLine("all task completed"); Console.ReadKey(); } } 使用TASK的waitAll可以达到同样的效果: var t1 = Task.Factory.StartNew(() => { Thread.Sleep(2000); Console.WriteLine("thread 1 complete"); }); var t2 = Task.Factory.StartNew(() => { Thread.Sleep(5000); Console.WriteLine("thread 2 complete"); }); var t3 = Task.Factory.StartNew(() => { Thread.Sleep(3000); Console.WriteLine("thread 3 complete"); }); Console.WriteLine("waiting tasks...."); Task.WaitAll(t1, t2, t3); Console.WriteLine("all task completed"); Console.ReadKey();
Ce qui précède est le contenu de l'utilisation de CountDownEvent en c#. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois. (www.php.cn) !