一个实例明白AutoResetEvent和 ManulResetEvent的用法
2017-06-06 11:43
459 查看
先看一段代码:
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public class WaitHandlerExample {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static AutoResetEvent waitHandler;
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static ManualResetEvent manualWaitHandler;
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadPoolMain() {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
waitHandler = new AutoResetEvent(false);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler = new ManualResetEvent(false);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
// Queue the task.
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Main thread does some work, then waiting....");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.WaitOne();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.WaitOne();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Main thread exits.");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
// This thread procedure performs the task.
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadProc(Object stateInfo) {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Thread.Sleep(1000);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Hello from the thread pool.");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Set(); //
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.Set();//过去了,但是没关,也就是说 信号还是开着的。
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//manualWaitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadProc2(object stateInfo)
{
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Thread.Sleep(100);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Hello from the thread Pool2");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Set();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.Set();//过去了,但是没有关
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
如果把 AutoResetEvent 比作 北京地铁的门闸,那么
AutoResetEvent waitHandler=new AutoResetEvent(false);
可以看作,初始化闸机口为关闭状态,
waitHandler.WaitOne();
可以看作刷卡
waitHandler.Set()
表示通过,并且闸机自动关闭(AutoReset)为下次通过做准备。他的一个重大的好处,就是线程只能一个一个通过,保持了顺序又避免了死锁。
如果使用manualResetEvent 呢,那么在waitHandler.Set 之后,必须调用Reset()方法,为下面一位进去做好准备, 否则,就相当于无法再次刷卡。
体现在程序中就是,AutoResetEvent 可以WaitOne 很多次,可是ManualResetEvent 如果不Reset
下次就不能使用. 这就是他们的区别。
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public class WaitHandlerExample {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static AutoResetEvent waitHandler;
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static ManualResetEvent manualWaitHandler;
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadPoolMain() {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
waitHandler = new AutoResetEvent(false);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler = new ManualResetEvent(false);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
// Queue the task.
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2));
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Main thread does some work, then waiting....");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.WaitOne();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.WaitOne();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Main thread exits.");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
// This thread procedure performs the task.
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadProc(Object stateInfo) {
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Thread.Sleep(1000);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Hello from the thread pool.");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Set(); //
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.Set();//过去了,但是没关,也就是说 信号还是开着的。
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//manualWaitHandler.Reset();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
public static void ThreadProc2(object stateInfo)
{
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Thread.Sleep(100);
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
Console.WriteLine("Hello from the thread Pool2");
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
//waitHandler.Set();
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
manualWaitHandler.Set();//过去了,但是没有关
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
![](http://xhinker.blog.51cto.com/images/editer/InBlock.gif)
}
如果把 AutoResetEvent 比作 北京地铁的门闸,那么
AutoResetEvent waitHandler=new AutoResetEvent(false);
可以看作,初始化闸机口为关闭状态,
waitHandler.WaitOne();
可以看作刷卡
waitHandler.Set()
表示通过,并且闸机自动关闭(AutoReset)为下次通过做准备。他的一个重大的好处,就是线程只能一个一个通过,保持了顺序又避免了死锁。
如果使用manualResetEvent 呢,那么在waitHandler.Set 之后,必须调用Reset()方法,为下面一位进去做好准备, 否则,就相当于无法再次刷卡。
体现在程序中就是,AutoResetEvent 可以WaitOne 很多次,可是ManualResetEvent 如果不Reset
下次就不能使用. 这就是他们的区别。
相关文章推荐
- 转:一个实例明白AutoResetEvent和ManulResetEvent的用法
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
- 转:一个实例明白AutoResetEvent和 ManulResetEvent的用法
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法 推荐
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
- 一个实例明白AutoResetEvent和 ManulResetEvent的用法
- ManulResetEvent与AutoResetEvent
- ManualResetEvent 用法
- 通过一个实例介绍 ArrayList 中常用API的用法
- php中current、next与reset函数用法实例
- php中current、next与reset函数用法实例
- ManualResetEvent和AutoResetEvent的区别
- synchronized用法的一个实例
- C# ManualResetEvent和AutoResetEvent 使用笔记
- BitBlt函数的用法和一个小实例
- AutoResetEvent和ManualResetEvent
- AutoResetEvent 与 ManualResetEvent<转>
- CreateEvent/OpenEvent/SetEvent/ResetEvent/WaitForSingleObject 相关用法说明