using System; using System.Threading; class Program { static CountdownEvent countdown = new CountdownEvent(3); static void Main() { new Thread(DoWork).Start(); new Thread(DoWork).Start(); new Thread(DoWork).Start(); countdown.Wait(); Console.WriteLine("All threads completed their work."); } static void DoWork() { Console.WriteLine("Thread {0} starting work...", Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); Console.WriteLine("Thread {0} completed work.", Thread.CurrentThread.ManagedThreadId); countdown.Signal(); } }
using System; using System.Threading; class Program { static CountdownEvent countdown = new CountdownEvent(2); static void Main() { new Thread(DoWork).Start(); new Thread(DoWork).Start(); if (countdown.Wait(5000)) { Console.WriteLine("All threads completed their work."); } else { Console.WriteLine("Timed out before all threads completed their work."); } } static void DoWork() { Console.WriteLine("Thread {0} starting work...", Thread.CurrentThread.ManagedThreadId); Thread.Sleep(1000); Console.WriteLine("Thread {0} completed work.", Thread.CurrentThread.ManagedThreadId); countdown.Signal(); } }The package library for System.Threading.CountdownEvent is "System.Threading" in C#.