You could use something like this. I should allow all threads to run apox 30 seconds then abort them if they have not completed. class Program { private static AutoResetEvent e = new AutoResetEvent(false); //No delay public static void DoWork1() { Console.WriteLine("DoWork1 Completed"); e.Set(); } //Long but completes public static void DoWork2() { Thread.Sleep(new TimeSpan(0, 0, 25)); Console.WriteLine("DoWork2 Completed"); e.Set(); } //runs too long public static void DoWork3() { Thread.Sleep(new TimeSpan(0, 0, 60)); Console.WriteLine("DoWork3 Completed"); e.Set(); } static void Main(string[] args) { //Create array of worker threads Thread[] workers = new Thread[3]; (workers[0] = new Thread(new ThreadStart(DoWork1))).Start(); (workers[1] = new Thread(new ThreadStart(DoWork2))).Start(); (workers[2] = new Thread(new ThreadStart(DoWork3))).Start(); DateTime abortTime = DateTime.Now.AddSeconds(30); //Wait until timout expires or all all thread are //completed. while (DateTime.Now < abortTime) { //Waits for 10 seconds on until any thread completes e.WaitOne(new TimeSpan(0, 0, 10)); int running = 0; foreach (Thread t in workers) { if (t.ThreadState != System.Threading.ThreadState.Stopped) { running++; } } if (running == 0) { break; } else { Console.WriteLine("Running threads {0}", running); } } //Kill off the threads that have not stopped foreach (Thread t in workers) { try { t.Abort(); } catch { Console.WriteLine("Aborted"); } } } }
W
wightman_michael
@wightman_michael