文章转载自:C# AutoResetEvent

AutoResetEvent 常常被用来在两个线程之间进行信号发送

AutoResetEvent是.net线程简易同步方法中的一种,两个线程共享相同的AutoResetEvent对象,线程可以通过调用AutoResetEvent对象的WaitOne()方法进入等待状态,然后另外一个线程通过调用AutoResetEvent对象的Set()方法取消等待的状态。

AutoResetEvent如何工作的

在内存中保持着一个bool值,如果bool值为False,则使线程阻塞,反之,如果bool值为True,则使线程退出阻塞。当我们创建AutoResetEvent对象的实例时,我们在函数构造中传递默认的bool值,以下是实例化AutoResetEvent的例子。

AutoResetEvent autoResetEvent = new AutoResetEvent(false);

WaitOne 方法

该方法阻止当前线程继续执行,并使线程进入等待状态以获取其他线程发送的信号。WaitOne将当前线程置于一个休眠的线程状态。WaitOne方法收到信号后将返回True,否则将返回False。

autoResetEvent.WaitOne();

WaitOne方法的第二个重载版本是等待指定的秒数。如果在指定的秒数后,没有收到任何信号,那么后续代码将继续执行。

static void ThreadMethod()
{
while(!autoResetEvent.WaitOne(TimeSpan.FromSeconds(2)))
{
Console.WriteLine("Continue");
Thread.Sleep(TimeSpan.FromSeconds(1));
} Console.WriteLine("Thread got signal");
}

这里我们传递了2秒钟作为WaitOne方法的参数。在While循环中,autoResetEvent对象等待2秒,然后继续执行。当线程取得信号,WaitOne返回为True,然后退出循环,打印"Thread got signal"的消息。

Set 方法

AutoResetEvent Set方法发送信号到等待线程以继续其工作,以下是调用该方法的格式。

autoResetEvent.Set();

 

AutoResetEvent例子

下面的例子展示了如何使用AutoResetEvent来释放线程。在Main方法中,我们用Task Factory创建了一个线程,它调用了GetDataFromServer方法。调用该方法后,我们调用AutoResetEvent的WaitOne方法将主线程变为等待状态。在调用GetDataFromServer方法时,我们调用了AutoResetEvent对象的Set方法,它释放了主线程,并控制台打印输出dataFromServer方法返回的数据。

class Program
{
static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
static string dataFromServer = ""; static void Main(string[] args)
{
Task task = Task.Factory.StartNew(() =>
{
GetDataFromServer();
}); //Put the current thread into waiting state until it receives the signal
autoResetEvent.WaitOne(); //Thread got the signal
Console.WriteLine(dataFromServer);
} static void GetDataFromServer()
{
//Calling any webservice to get data
Thread.Sleep(TimeSpan.FromSeconds(4));
dataFromServer = "Webservice data";
autoResetEvent.Set();
}
}

C# AutoResetEvent的更多相关文章

  1. C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...

  2. AutoResetEvent ManualResetEvent WaitOne使用注意事项

    公司还用这些老家伙没办法,用了几次这俩.每次用都要重新翻一下A片. 好好的A片楞是翻译成了禅经.把这东西弄成个玄学.微软也是吃枣药丸.参考了@风中灵药的blog.写的牛逼. 还有一些公司用到的风中灵药 ...

  3. 多线程AutoResetEvent

    我们在线程编程的时候往往会涉及到线程的通信,通过信号的接受来进行线程是否阻塞的操作. AutoResetEvent 允许线程通过发信号互相通信.通常,此通信涉及线程需要独占访问的资源. AutoRes ...

  4. 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent

    本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...

  5. C#线程同步自动重置事件——AutoResetEvent

    AutoResetEvent对象用来进行线程同步操作,AutoResetEvent类继承waitHandle类. AutoResetEvent对象有终止和非终止两种状态,终止状态是线程继续执行,非终止 ...

  6. C#多线程同步事件及等待句柄AutoResetEvent 和 ManualResetEvent

    最近捣鼓了一下多线程的同步问题,发现其实C#关于多线程同步事件处理还是很灵活,这里主要写一下,自己测试的一些代码,涉及到了AutoResetEvent 和 ManualResetEvent,当然还有也 ...

  7. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  8. C#读写者线程(用AutoResetEvent实现同步)(转载)

    C#读写者线程(用AutoResetEvent实现同步) 1. AutoResetEvent简介 通知正在等待的线程已发生事件.无法继承此类. 常用方法简介: AutoResetEvent(bool ...

  9. C# ManualResetEvent和AutoResetEvent 使用笔记

    一.两者区别 1.ManualResetEvent 调用一次Set()后将允许恢复所有被阻塞线程.需手动在调用WaitOne()之后调用Reset()重置信号量状态为非终止,然后再次调用WaitOne ...

  10. [c#基础]AutoResetEvent

    摘要 AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件.此类不能被继承.也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了. AutoResetEvent 该 ...

随机推荐

  1. ADC for programmable logic uses one capacitor

    Many electronic devices require user input for setting the application properties. Typical input dev ...

  2. NativeXml: A native Delphi XML parser and writer

    http://www.simdesign.nl/xml.html This software component contains a small-footprint Object Pascal (D ...

  3. IDA 中文字符串

    http://www.pediy.com/kssd/pediy05/pediy50528.htm Ida Pro 的默认设置里对中文字串的支持比较差,对于首字节大于'E0'的都显示成?了.其实...  ...

  4. SpringMvc(1) --Eclipse搭建web项目

    http://blog.csdn.net/uucai http://blog.csdn.net/uucai/article/details/21258575

  5. MySQL MERGE存储引擎 简介

    MERGE存储引擎把一组MyISAM数据表当做一个逻辑单元来对待,让我们可以同时对他们进行查询.构成一个MERGE数据表结构的各成员MyISAM数据表必须具有完全一样的结构.每一个成员数据表的数据列必 ...

  6. SharePoint Server 2013 Offline Installation (without Internet)

    转自:http://social.msdn.microsoft.com/Forums/sharepoint/zh-CN/08f90e0f-1f52-4eba-9f6e-4dd635ffaadc/sha ...

  7. Memcached源码分析——连接状态变化分析(drive_machine)

    这篇文章主要介绍Memcached中,基于libevent构造的主线程和worker线程所处理连接的状态互相转换的过程(不涉数据的存取等操作),也就是drive_machine的主要业务逻辑了.状态转 ...

  8. 企业应用:浅谈 “数据权限” 和 查询 API 设计

    背景 多数企业应用都需要对数据权限进行控制,如:某个用户只能看到某个范围的数据(数据行).某个用户只能看到某几列数据(数据列).本文以数据行级别的权限控制为范例,谈谈如何设计权限模型和查询 API. ...

  9. php 将网页执行的输出写入到本地文件中

    php -f /var/www/html/default/script/lol_score_calculate/calculate.php >>score_calcutelate.log

  10. C#启动外部程序以及等待外部程序关闭的几种方法

    1. 启动外部程序,不等待其退出. 2. 启动外部程序,等待其退出. 3. 启动外部程序,无限等待其退出. 4. 启动外部程序,通过事件监视其退出. // using System.Diagnosti ...