日期:2014-05-17 浏览次数:20831 次
private static AutoResetEvent[] autoEvents = new AutoResetEvent[10];
public static void Run()
{
int index = int.Parse(Thread.CurrentThread.Name);
Console.WriteLine(string.Format("当前时间{0},当前线程{1}", DateTime.Now, index));
autoEvents[index].Set(); <---这里
}
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
autoEvents[i] = new AutoResetEvent(false);
Thread t = new Thread(new ThreadStart(Run));
t.Name = i.ToString();
t.Start();
}
WaitHandle.WaitAll(autoEvents); <---这里
Console.WriteLine("所有线程都结束完毕");
Console.ReadKey();
}