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的线程同 ...
随机推荐
- 基于jQuery的自定义滚动条
在线演示 本地下载
- jquery全屏图片滑动切换
在线演示 本地下载
- js表格拖拽
html部分 <div id="chenkbox"> <div id="tableSort"> <ol> <li> ...
- 【转载】CPU相关总结
What is the difference between Processor, Core, Logical Processor ? Processor : It’s the physical co ...
- js赋值符号“=”的小例子
var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...
- LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. ...
- hdu5131 贪心
#include<stdio.h> #include<string.h> #include<algorithm> #include<string> #i ...
- hdu5441 并查集 长春网赛
对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin ...
- Hibernate错误——No row with the given identifier exists
错误 是用的是Hibernate自动建立的数据表,在进行数据库操作时,出现错误No row with the given identifier exists 解决 关系数据库一致性遭到了破坏,找到相关 ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...