[. NET multithreading] Countdownevent

Source: Internet
Author: User

System.Threading.CountdownEvent is a synchronization primitive that, after receiving a certain number of signals, will unlock its waiting thread. Countdownevent has an initial count at initialization, which is called when each work item is completedSignal. each time Signal is called, the signal count decrements by 1. On the main thread, the call to Wait will block until the signal count is zero.
1 Static voidMain (string[] args)2 {3Countdownevent countdown =NewCountdownevent (3);4     intTaskID =1;5      for(vari =0; I <3; i++)6     {7Task.Factory.StartNew (() =8         {9             intCur = taskid++;TenConsole.WriteLine ($"Task[{cur}] is Running"); OneThread.Sleep (TaskID * +); A Countdown. Signal (); -Console.WriteLine ($"Task[{cur}] Exit"); -         }); the     } -Console.WriteLine ($"Main Wait"); - Countdown. Wait (); -Console.WriteLine ($"Main Exit"); + Console.readkey (); -}
countdownevent Sample Code

Countdownevent is similar to barrier, but it is more convenient to use barrier in multiple waits.

[. NET multithreading] Countdownevent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.