AutoResetEvent是.net线程简易同步方法中的一种。

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

两个线程共享相同的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 signalautoResetEvent.WaitOne();//Thread got the signalConsole.WriteLine(dataFromServer);}static void GetDataFromServer(){//Calling any webservice to get dataThread.Sleep(TimeSpan.FromSeconds(4));dataFromServer = "Webservice data";autoResetEvent.Set();}
}

C# AutoResetEvent相关推荐

  1. AutoResetEvent和ManualResetEvent

    首先说说线程的终止状态和非终止状态.AutoResetEvent和ManualResetEvent的构造函数中,都有bool变量来指明线程的终止状态和非终止状态.true表示终止状态,false表示非 ...

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

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

  3. 线程同步-AutoResetEvent

    线程同步的其他方式: 1.AutoResetEvent 使用WaitOne和set 进行阻塞和释放阻塞的线程, WaitOne方法定义: public virtual bool WaitOne(int ...

  4. 【深度好文】多线程之WaitHandle--派生EventWaitHandle事件构造-》AutoResetEvent、ManualResetEvent...

    AutoResetEvent/ManualResetEvent 都是继承自 EventWaitHandle ,EventWaitHandle继承自WaitHandle. 在讨论这个问题之前,我们先了解 ...

  5. 关于AutoResetEvent 和ManualResetEvent

    在公司的代码里面看到这两个类的使用了,第一次去msdn上的时候,看的一头雾水(关键是没有找对方法) http://www.cnblogs.com/qingyun163/archive/2013/01/ ...

  6. autoresetevent java_[原创]AutoResetEvent, ManualResetEvent的Java模拟

    AutoResetEvent, ManualResetEvent是C#中常用的线程同步方法,在Java中可以模拟,AutoResetEvent使用Semaphore,增加的是许可证数量,程序里只有一个 ...

  7. c# AutoResetEvent和ManualResetEvent

    网上有很多AutoResetEvent和ManualResetEvent的详细介绍,在这里不做过多详细的解释,写下自己的一点心得留作备忘. AutoResetEvent和ManualResetEven ...

  8. ManulResetEvent与AutoResetEvent

    定义 ManualResetEvent:通知正在等待的线程已发生事件. AutoResetEvent:通知正在等待的线程已发生事件. 代码 ManualResetEvent测试 using Syste ...

  9. Thread 等待异步方法执行完成方案:AutoResetEvent.WaitOne()

    Thread 等待异步方法执行完成方案:AutoResetEvent.WaitOne() static AutoResetEvent myresetevent = new AutoResetEvent ...

最新文章

  1. LeetCode简单题之寻找数组的中心下标
  2. ASP:关于生成HTML文件的新闻系统
  3. ResourceLoader 获取资源
  4. 息系统项目管理师知识点:区块链
  5. python中thread的setDaemon、join的用法
  6. LeetCode MySQL 1075. 项目员工 I
  7. 罗永浩吐槽卖小米、苹果被骂,卖华为也被骂,李楠:警惕键盘侠
  8. 实现自定义扩展点_spring扩展API接口介绍
  9. maven聚合工程打包_史诗级!有手就会的Maven教程(八)
  10. 自动简单,完美解决 vissim4.3 每次启动修改时间问题!
  11. thinkphp6 task异步
  12. mysql常用客户端工具
  13. 德标螺纹规格对照表_(完整word版)德标与国标对照表
  14. mockserver使用
  15. 数字图像处理——LoG算子
  16. Open Drain vs Push Pull
  17. 银河麒麟安装docker-compose体验
  18. Centos7模板机制作
  19. (66)-- 多进程爬取腾讯招聘信息
  20. python 一个例子解释全局变量和局部变量

热门文章

  1. OpenCV iOS-图像处理
  2. OpenGL绘制Triangle三角形
  3. C++实现冒泡排序(附完整源码)
  4. QT的QDBusArgument类的使用
  5. C++对C的加强之 “实用性”增加
  6. C++::CPLEX文件读写
  7. 极光推送 请检查参数合法性_极光(JG.US)牵手中国人寿数据中心,合作助推智能用户运营_行业动态...
  8. 支付宝支付-提现到个人支付宝
  9. Hive内置运算函数,自定义函数(UDF)和Transform
  10. 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)