AutoResetEvent与区别
private AutoResetEvent autoEvent = new AutoResetEvent(False);
Initializes a new instance of the AutoResetEvent class with a Boolean value indicating whether to set the initial state to signaled. (MSDN)
即:new AutoRestEvent(True) 时 ,当第一次使用 autoEvent.WaitOne() 时State已为Ture,将直接通过。
AutoResetEvent与ManualResetEvent 的区别是:
AutoResetEvent通过.WaitOne()重置state为False。而ManualResetEvent使用.WaitOne()后保留state为True的状态,止到执行.Reset()。
.Set() 将State置Ture
.WaitOne()阻止所在线程止到State为True时通过。(通过后AutoResetEvent将State置False,而ManualResetEvent不会)
.Reset() 将State置False
.WaitOne(2000) 阻止所在线程止到发生.Set() 或 2秒钟后自动将State置为True并通过。
所以:
可能发生先执行.Set(),然后执行.WaitOne()时直接通过的现象。
另外:.WaitOne(10000,true);
true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it afterward; otherwise, false (针对Lock的原子操作)。
以下转自 http://hi.baidu.com/jerrycc/blog/item/9017d41767d1f756f3de3224.html 我心飞翔
// 在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别。Set方法将信号置为发送状态,Reset方法将信号置为不发送状态,WaitOne等待信号的发送。可以通过构造函数的参数值来决定其初始状态,若为true则非阻塞状态,为false为阻塞状态。如果某个线程调用WaitOne方法,则当信号处于发送状态时,该线程会得到信号, 继续向下执行。其区别就在调用后,AutoResetEvent.WaitOne()每次只允许一个线程进入,当某个线程得到信号后,AutoResetEvent会自动又将信号置为不发送状态,则其他调用WaitOne的线程只有继续等待.也就是说,AutoResetEvent一次只唤醒一个线程;而ManualResetEvent则可以唤醒多个线程,因为当某个线程调用了ManualResetEvent.Set()方法后,其他调用WaitOne的线程获得信号得以继续执行,而ManualResetEvent不会自动将信号置为不发送。也就是说,除非手工调用了ManualResetEvent.Reset()方法,则ManualResetEvent将一直保持有信号状态,ManualResetEvent也就可以同时唤醒多个线程继续执行。
//ManualResetEvent类,AutoResetEvent类
//这两个类都是由EventWaitHandle类派生出来的,所以功能和调用方法都很相似。
//这两个类常用于阻断某个线程的执行,然后在符合条件的情况下再恢复其执行。
//举个例子,你想送花给一个MM,托了一个送花的小伙子送了过去,而你希望当MM收到花之后就立即打个电话过去告诉她。
//但问题是你不知道花什么时候才送到MM的手里,打早了打迟了都不好,这时你可以使用ManualResetEvent对象帮忙。当委
//托小伙子送花过去的时候,使用ManualResetEvent的WaitOne方法进行等待。当小伙子把花送到MM的手中时,再调用一下
//ManualResetEvent的Set方法,你就可以准时地打电话过去了。
//另外ManualResetEvent还有一个Reset方法,用来重新阻断调用者执行的,情况就好比你委托了这个小伙子送花给N个MM,
//而又想准时地给这N个MM打电话的情况一样。
using System;
using System.Threading;
public class TestMain
{
private static ManualResetEvent ent = new ManualResetEvent(false);
public static void Main()
{
Boy sender = new Boy(ent);
Thread th = new Thread(new ThreadStart(sender.SendFlower));
th.Start();
ent.WaitOne(); //等待工作
Console.WriteLine("收到了吧,花是我送嘀:)");
Console.ReadLine();
}
}
public class Boy
{
ManualResetEvent ent;
public Boy(ManualResetEvent e)
{
ent = e;
}
public void SendFlower()
{
Console.WriteLine("正在送花的途中");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.Write("..");
}
Console.WriteLine("\r\n花已经送到MM手中了,boss");
ent.Set(); //通知阻塞程序
}
}
//而AutoResetEvent类故名思意,就是在每次Set完之后自动Reset。让执行程序重新进入阻塞状态。
//即AutoResetEvent.Set() 相当于 ManualResetEvent.Set() 之后又立即 ManualResetEvent.Reset(),
//其他的就没有什么不同的了。
//举个送花给N个MM的例子:
using System;
using System.Threading;
public class TestMain
{
private static AutoResetEvent ent = new AutoResetEvent(false);
public static void Main()
{
Boy sender = new Boy(ent);
for (int i = 0; i < 3; i++)
{
Thread th = new Thread(new ThreadStart(sender.SendFlower));
th.Start();
ent.WaitOne(); //等待工作
Console.WriteLine("收到了吧,花是我送嘀:)\r\n\r\n");
}
Console.ReadLine();
}
}
public class Boy
{
AutoResetEvent ent;
public Boy(AutoResetEvent e)
{
ent = e;
}
public void SendFlower()
{
Console.WriteLine("正在送花的途中");
for (int i = 0; i < 10; i++)
{
Thread.Sleep(200);
Console.Write("..");
}
Console.WriteLine("\r\n花已经送到MM手中了,boss");
ent.Set(); //通知阻塞程序,这里的效果相当于 ManualResetEvent的Set()方法+Reset()方法
}
}
//要注意的是ManualResetEvent和AutoResetEvent 的构造函数都有一个bool的参数,用这个参数可以指定初始情况下,同步对象的处于阻塞(设置为false)还是非阻塞(设置为true)的状态。
//另外WaitOne方法也可以带两个参数:
//WaitOne (int millisecondsTimeout,bool exitContext)
//millisecondsTimeout:等待的毫秒数,或为 Timeout.Infinite (-1),表示无限期等待。
//exitContext:为 true,则等待之前先退出上下文的同步域(如果在同步上下文中),然后在稍后重新获取它;否则为false。
//就是说,等待是可以加上一个期限的,如果等待的同步对象一直都不Set()的话,那么程序就会卡死,所以在WaitOne方法里面可以放置一个时间期限,单位是毫秒。
浙公网安备 33010602011771号