static void Main(string[] args) { List<ManualResetEvent> manualEvents = new List<ManualResetEvent>(); for(int i=0;i<3;i++) { ManualResetEvent mre = new ManualResetEvent(false); manualEvents.Add(mre); Param para = new Param() { mre = mre, Para = i + 1 }; //ThreadPool.QueueUserWorkItem(ThreadMethod, para); ThreadPool.QueueUserWorkItem(state => { try { ThreadMethod(para); } catch (Exception ex) { OnEx(ex); } }); } WaitHandle.WaitAll(manualEvents.ToArray()); Console.WriteLine("Hit any key to continue"); Console.ReadKey(); }
public class Param { public ManualResetEvent mre; public int Para; }
private static void OnEx(Exception ex) { Console.WriteLine(ex.Message); } private static void ThreadMethod(object state) { Param para = (Param)state; for (int i = 0; i < 7; i++) { Console.WriteLine("In thread " + para.Para); if (i == 4) throw new Exception("HAHA"); Thread.Sleep(1000); } para.mre.Set(); }