日期:2014-05-18 浏览次数:20890 次
using System; using System.Collections.Generic; using System.Text; using System.Threading; namespace ThreadWait { class StataInfo { public int index; public ManualResetEvent manualEvent; public StataInfo(int index, ManualResetEvent manualEvent) { this.index = index; this.manualEvent = manualEvent; } } class Program { static void Main(string[] args) { ManualResetEvent[] manualEvents = new ManualResetEvent[6]; for (int i = 0; i < 6; i++) { Thread thread = new Thread(new ParameterizedThreadStart(ThreadWork)); manualEvents[i] = new ManualResetEvent(false); StataInfo stataInfo = new StataInfo(i,manualEvents[i]); thread.Start(stataInfo); } WaitHandle.WaitAll(manualEvents); Console.WriteLine("All thread have exit!."); Console.Read(); } static void ThreadWork(object stata) { for(int i=0;i<10;++i) { Thread.Sleep(200); } StataInfo stataInfo = (StataInfo)stata; Console.WriteLine(string.Format("The sub thread exit.",stataInfo.index)); ManualResetEvent manualEvent = (ManualResetEvent)stataInfo.manualEvent; manualEvent.Set(); } } }