、、

AutoResetEvent内存中保持着一个bool值
False,则使线程阻塞;值为True,使线程退出阻塞

创建AutoResetEvent对象的实例,在函数构造中传递默认的bool值;
AutoResetEvent autoResetEvent = new AutoResetEvent(false);

autoResetEvent.Reset();  //修改bool值为False;

autoResetEvent.Set(); //修改bool值为True;
--------------------------------------------------------------------

WaitOne() 方法,阻止 线程 继续执行进入等待状态;收到信号返回true,否则返回false;
WaitOne(毫秒),等待指定秒数,如果指定秒数收到任何信号,那么后续代码继续执行

  1. AutoResetEvent autoResetEvent = new AutoResetEvent(false);//只对一个线程设置为有信号。
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. CheckForIllegalCrossThreadCalls = false;
  6. }
  7. private void Form1_Load(object sender, EventArgs e){ }
  8. private void buttonSet_Click(object sender, EventArgs e)
  9. {
  10. autoResetEvent.Set();//设置为True 时 WriteMsg() 会跳出循环,停止输出:Continue;
  11. }
  12. private void buttonStart_Click(object sender, EventArgs e)
  13. {
  14. Thread td = new Thread(WriteMsg);
  15. td.Start();
  16. }
  17. /// <summary>
  18. /// 输出消息
  19. /// </summary>
  20. public void WriteMsg()
  21. {
  22. autoResetEvent.Set();
  23. autoResetEvent.WaitOne();
  24. //遇到此行代码,如果autoResetEvent为true,会直接往下执行代码,autoResetEvent为false,会停止在此行代码;
  25. //因为此行代码之前,执行了Set()方法,所以WaitOne()语句直接执行过。
  26. textBox1.AppendText("\r\nvvvvvvv");
  27. //此时,autoResetEvent 变成 false ;
  28. while (!autoResetEvent.WaitOne(TimeSpan.FromSeconds()))//原值为:false,加上!号变为:true ;
  29. {
  30. textBox1.AppendText("\r\nContinue");
  31. Thread.Sleep(TimeSpan.FromSeconds());
  32. }
  33. bool bl10 = autoResetEvent.WaitOne();
  34. //跳出循环后,执行到此行代码时,因为autoResetEvent又变为false,所以就停止在了此处,没有继续执行。
  35. //再次执行autoResetEvent.Set();方法后就会直接执行完成方法。
  36. }
  37. private void buttonReset_Click(object sender, EventArgs e)
  38. {
  39. autoResetEvent.Reset();
  40. }

---------------------------------------------------------------------

  1. /// <summary>
  2. /// 输出消息
  3. /// </summary>
  4. public void WriteMsg()
  5. {
  6. autoResetEvent.WaitOne(TimeSpan.FromSeconds());
  7. //如果没有Reset() 和 Set()方法被调用,当执行到此行代码时,会等待5秒钟,才会继续执行后面的代码;
  8. //如果执行了.Set()方法,就会立马继续执行后面代码,不会继续等待5秒之后。
  9. textBox1.AppendText("\r\nContinue");
  10. Thread.Sleep(TimeSpan.FromSeconds());
  11. }

‘’

C# AutoResetEvent 理解的更多相关文章

  1. 个人对AutoResetEvent和ManualResetEvent的理解(转载)

    仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...

  2. AutoResetEvent waitone set进一步理解补充

    AutoResetEvent 的定义 //定义两个信号锁 AutoResetEvent ReadTxt = new AutoResetEvent(false); AutoResetEvent Uplo ...

  3. AutoResetEvent和ManualResetEvent理解 z

    AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...

  4. C#理解AutoResetEvent和ManualResetEvent

    当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...

  5. C#深入理解AutoResetEvent和ManualResetEvent

    当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...

  6. AutoResetEvent和ManualResetEvent理解

    AutoResetEvent和ManualResetEvent用于多线程之间代码执行顺序的控制,它们继承自WaitHandle,API相同,但在使用中还是有区别的. 每次使用时虽然理解了,但由于没有去 ...

  7. 个人对AutoResetEvent和ManualResetEvent的理解

    一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方法的官方定义并不好理解,什么终止.非 ...

  8. [转]个人对AutoResetEvent和ManualResetEvent的理解

    仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...

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

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

随机推荐

  1. 基于jQuery的自定义滚动条

    在线演示 本地下载

  2. jquery全屏图片滑动切换

    在线演示 本地下载

  3. js表格拖拽

    html部分 <div id="chenkbox"> <div id="tableSort"> <ol> <li> ...

  4. 【转载】CPU相关总结

    What is the difference between Processor, Core, Logical Processor ? Processor : It’s the physical co ...

  5. js赋值符号“=”的小例子

    var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...

  6. 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. ...

  7. hdu5131 贪心

    #include<stdio.h> #include<string.h> #include<algorithm> #include<string> #i ...

  8. hdu5441 并查集 长春网赛

    对于每次询问的大的值,都是从小的值开始的,那就从小到大处理,省去很多时间,并且秩序遍历一遍m; 这题cin容易超时,scanf明显快很多很多.G++又比C++快; //这代码scanf400+,cin ...

  9. Hibernate错误——No row with the given identifier exists

    错误 是用的是Hibernate自动建立的数据表,在进行数据库操作时,出现错误No row with the given identifier exists 解决 关系数据库一致性遭到了破坏,找到相关 ...

  10. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...