ManualResetEvent就像一个信号灯,可以利用它的信号,控制当前线程是挂起状态还是运行状态。
        它有几个常用的方法:Reset(),Set(),WaitOne();
        初始化该对象时,可以指定其默认的状态(有信号/无信号);
        在初始化以后,该对象将保持原来的状态不变,直到它的Reset()或者Set()方法被调用;
        Reset()方法将其设置为无信号状态,Set()方法将其设置为有信号状态;
        WaitOne()方法在无信号状态下,可以使当前线程挂起;注意这里说的是当前线程;
        直到调用了Set()方法,该线程才被激活。

        在多线程的代码里,可以使用一个ManualResetEvent对象来控制线程所有线程;
        只要在调用WaitOne()方法前,调用Reset()方法,因为WaitOne()控制的是当前线程;
        但是这样做,ManualResetEvent对象的管理逻辑会变得复杂;
        所以这里建议一条线程一个ManualResetEvent对象。
 

再议ManualResetEventusing System;
再议ManualResetEvent
using System.Collections.Generic;
再议ManualResetEvent
using System.Text;
再议ManualResetEvent
using System.Threading;
再议ManualResetEvent
再议ManualResetEvent
namespace ConsoleThread
}

相关文章:

  • 2021-10-09
  • 2021-05-29
  • 2021-11-22
  • 2021-10-20
  • 2021-08-14
猜你喜欢
  • 2022-01-28
  • 2021-08-18
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案