CountdownEvent的注释为:

表示在计数变为零时处于有信号状态的同步基元

它是一个同步基元,它在收到一定次数的信号之后,将会解除对其等待线程的锁定。

简的来说就是,事先设置需要有多少个通知,等待指定的通知数量全部到达后,Wait()才继续往下运行。代码示例如下:

static void Main(string[] args)
{
    CountdownEvent countdownEvent = new CountdownEvent(4);
    for(int i = 1; i < 5; i++)
    {
        int v = i;
        Task.Run(() => {
            Thread.Sleep(v * 1000);
            countdownEvent.Signal();
            Console.WriteLine(v);
        });
    }
    countdownEvent.Wait();//等待4个信号全部来袭后,继续往下执行
    Console.WriteLine("完成咯");
    countdownEvent.Dispose();
    Console.ReadKey();
}

相关文章:

  • 2021-08-16
  • 2021-11-05
  • 2021-09-02
  • 2021-11-05
  • 2021-09-11
  • 2021-09-21
  • 2021-10-05
  • 2021-10-04
猜你喜欢
  • 2021-08-14
  • 2021-10-13
  • 2021-09-16
  • 2021-12-22
  • 2021-08-03
  • 2021-10-17
  • 2021-07-09
  • 2021-09-13
相关资源
相似解决方案