- 摘要:WaitHandle类的用处可以调用它的方法,来等待若干个信号发生,即可以调节多个线程之间的同步
在讨论WaitHandle类之前 我们先看一张MSDN上给的继承关系图:
红色框指出的部分,就是这篇文章要讨论的WaitHandle类,绿色框指出的部分就是本文提到的一些异步对象。
可以看到,异步对象的父类就是WaitHandle
异步对象如AutoResetEvent和ManualResetEvent定义了一种信号机制,这种机制用来表明是访问共享资源还是释放共享资源。
1. WaitHanle类MSDN上的定义
封装了一些对象,这些对象等待着独占访问共享资源
(Encapsulates operating system–specific objects that wait for exclusive access to shared resources.)
2.WaitHandle类的用处
可以调用它的方法,来等待若干个信号发生,即可以调节多个线程之间的同步。
代码示例:
static WaitHandle[] waitHandles =new WaitHandle[]
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
static Random r =new Random();
staticvoid Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for Both tasks to complete");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);//调用WaitHandle的方法来阻塞主线程,当前方法是当所有异步对象接到信号时退出阻塞
Console.WriteLine("Both task are completed (time waited={0})", (DateTime.Now - dt).TotalMilliseconds);
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);//调用WaitHandle的方法来阻塞主线程,当前方法是当任意一个异步对象接到信号时退出阻塞
Console.WriteLine("Task {0} finished first (time waited={1})",index+1,(DateTime.Now-dt).TotalMilliseconds);
Console.Read();
}
staticvoid DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent)state;
int time =1000* r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.",time);
Thread.Sleep(time);
are.Set();
}
{
new AutoResetEvent(false),
new AutoResetEvent(false)
};
static Random r =new Random();
staticvoid Main(string[] args)
{
DateTime dt = DateTime.Now;
Console.WriteLine("Main thread is waiting for Both tasks to complete");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
WaitHandle.WaitAll(waitHandles);//调用WaitHandle的方法来阻塞主线程,当前方法是当所有异步对象接到信号时退出阻塞
Console.WriteLine("Both task are completed (time waited={0})", (DateTime.Now - dt).TotalMilliseconds);
dt = DateTime.Now;
Console.WriteLine();
Console.WriteLine("The main thread is waiting for either task to complete");
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[0]);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoTask), waitHandles[1]);
int index = WaitHandle.WaitAny(waitHandles);//调用WaitHandle的方法来阻塞主线程,当前方法是当任意一个异步对象接到信号时退出阻塞
Console.WriteLine("Task {0} finished first (time waited={1})",index+1,(DateTime.Now-dt).TotalMilliseconds);
Console.Read();
}
staticvoid DoTask(Object state)
{
AutoResetEvent are = (AutoResetEvent)state;
int time =1000* r.Next(2, 10);
Console.WriteLine("Performing a task for {0} milliseconds.",time);
Thread.Sleep(time);
are.Set();
}
代码中我们可以看到 主线程中调用了 WaitHandle.WaitAll() 和WaitHandle.WaitAny(),分别是等待所有的信号变为终止状态和任何一个信号变为
终止状态。
执行结果:
从执行结果中可以看出,第一部分,主线程最后显示执行时间是在两个任务都执行完成后显示的。 第二部分,则在两个任务中只要有一个执行完毕,主线程就显示执行时间。