110,499
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
//定义同步事件
AutoResetEvent[] events =
{
new AutoResetEvent(false),
new AutoResetEvent(false),
new AutoResetEvent(false),
new AutoResetEvent(false)
};
Action notify = delegate
{
//通知,等待全部完成才输出信息
if (WaitHandle.WaitAll(events, 0))
{
Console.WriteLine("All threads finished");
}
};
//线程,把你的处理代码放在这里
WaitCallback wcb = (status) =>
{
//to do, you time cost codes
Random r = new Random((int)DateTime.Now.Ticks);
Thread.Sleep(r.Next(100,1000));
Console.WriteLine("Thread finished");
//Event置位,表明处理完成
AutoResetEvent are = status as AutoResetEvent;
are.Set();
//通知
notify();
};
//放入线程池,每个Thread关联一个AutoResetEvent
foreach (AutoResetEvent are in events)
{
ThreadPool.QueueUserWorkItem(wcb, are);
}
}