C# AutoResetEvent 理解
、、
AutoResetEvent在内存中保持着一个bool值
值为False,则使线程阻塞;值为True,使线程退出阻塞;
创建AutoResetEvent对象的实例,在函数构造中传递默认的bool值;
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
autoResetEvent.Reset(); //修改bool值为False;
autoResetEvent.Set(); //修改bool值为True;
--------------------------------------------------------------------
WaitOne() 方法,阻止 线程 继续执行,进入等待状态;收到信号返回true,否则返回false;
WaitOne(毫秒),等待指定秒数,如果指定秒数后,没有收到任何信号,那么后续代码将继续执行;
AutoResetEvent autoResetEvent = new AutoResetEvent(false);//只对一个线程设置为有信号。
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e){ }
private void buttonSet_Click(object sender, EventArgs e)
{
autoResetEvent.Set();//设置为True 时 WriteMsg() 会跳出循环,停止输出:Continue;
}
private void buttonStart_Click(object sender, EventArgs e)
{
Thread td = new Thread(WriteMsg);
td.Start();
}
/// <summary>
/// 输出消息
/// </summary>
public void WriteMsg()
{
autoResetEvent.Set();
autoResetEvent.WaitOne();
//遇到此行代码,如果autoResetEvent为true,会直接往下执行代码,autoResetEvent为false,会停止在此行代码;
//因为此行代码之前,执行了Set()方法,所以WaitOne()语句直接执行过。
textBox1.AppendText("\r\nvvvvvvv");
//此时,autoResetEvent 变成 false ;
while (!autoResetEvent.WaitOne(TimeSpan.FromSeconds()))//原值为:false,加上!号变为:true ;
{
textBox1.AppendText("\r\nContinue");
Thread.Sleep(TimeSpan.FromSeconds());
}
bool bl10 = autoResetEvent.WaitOne();
//跳出循环后,执行到此行代码时,因为autoResetEvent又变为false,所以就停止在了此处,没有继续执行。
//再次执行autoResetEvent.Set();方法后就会直接执行完成方法。
}
private void buttonReset_Click(object sender, EventArgs e)
{
autoResetEvent.Reset();
}
---------------------------------------------------------------------
/// <summary>
/// 输出消息
/// </summary>
public void WriteMsg()
{
autoResetEvent.WaitOne(TimeSpan.FromSeconds());
//如果没有Reset() 和 Set()方法被调用,当执行到此行代码时,会等待5秒钟,才会继续执行后面的代码;
//如果执行了.Set()方法,就会立马继续执行后面代码,不会继续等待5秒之后。
textBox1.AppendText("\r\nContinue");
Thread.Sleep(TimeSpan.FromSeconds());
}
‘’
C# AutoResetEvent 理解的更多相关文章
- 个人对AutoResetEvent和ManualResetEvent的理解(转载)
仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...
- AutoResetEvent waitone set进一步理解补充
AutoResetEvent 的定义 //定义两个信号锁 AutoResetEvent ReadTxt = new AutoResetEvent(false); AutoResetEvent Uplo ...
- AutoResetEvent和ManualResetEvent理解 z
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- C#理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...
- C#深入理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...
- AutoResetEvent和ManualResetEvent理解
AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...
- 个人对AutoResetEvent和ManualResetEvent的理解
一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方法的官方定义并不好理解,什么终止.非 ...
- [转]个人对AutoResetEvent和ManualResetEvent的理解
仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...
- 多线程中的锁系统(三)-WaitHandle、AutoResetEvent、ManualResetEvent
本章主要介绍下基于内核模式构造的线程同步方式,事件,信号量. 阅读目录: 理论 WaitHandle AutoResetEvent ManualResetEvent 总结 理论 Windows的线程同 ...
随机推荐
- 重磅!容器集群监控利器 阿里云Prometheus 正式免费公测
Prometheus 作为容器生态下集群监控的首选方案,是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,并于 2015 年正式发布.2016 年,Prometheu ...
- 提取网址的python练习
import urllib, urllib2, cookielib from HTMLParser import HTMLParser import sys reload(sys) sys.setde ...
- 「WC2018」即时战略
「WC2018」即时战略 考虑对于一条链:直接随便找点,然后不断问即可. 对于一个二叉树,树高logn,直接随便找点,然后不断问即可. 正解: 先随便找到一个点,问出到1的路径 然后找别的点,考虑问出 ...
- thinkphp5.0 模板包含文件
在index.html里包含layout.html:{include file=“layout”}它这里是以绝对路径查找所包含的文件,默认是view目录下 在这种情况下,要在在index.html里包 ...
- 文字内容展开与折叠jquery代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP程序连接Redis报read error on connection问题
线上PHP程序动不动就报PHP Fatal error: Uncaught RedisException: read error on connection错误,就是连接Redis在那么1秒钟有问题, ...
- ROS出现“Couldn't find executable named listener below //home/xxx/catkin_ws/src/mypack”问题
在执行节点时出现了如下图所示的错误: 错误原因是在路径下找不到可执行的节点文件.但事实是已经对工作空间进行了编译,并且在devel /lib/my_serial_node 中已经生成了可执行文件. 如 ...
- @codeforces - 414E@ Mashmokh's Designed Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵 n 个点的树,每个点的儿子是有序的. 现给定 m 次操 ...
- @loj - 6354@「CodePlus 2018 4 月赛」最短路
目录 @description@ @solution@ @accepted code@ @details@ @description@ 企鹅国中有 N 座城市,编号从 1 到 N . 对于任意的两座城 ...
- mysql数据库之linux版本
http://repo.mysql.com/yum/mysql-5.6-community/ 安装 ...