Use of CountDownEvent in c#

黄舟
Release: 2017-03-01 10:50:53
Original
2409 people have browsed it

The use of CountDownEvent in c

#
class Program
    {
        static CountdownEvent _count = new CountdownEvent(3);
        static void Main(string[] args)
        {
          
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("thread 1 complete");
                _count.Signal();
            });
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(5000);
                Console.WriteLine("thread 2 complete");
                _count.Signal();
            });
            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(3000);
                Console.WriteLine("thread 3 complete");
                _count.Signal();
            });

            Console.WriteLine("waiting tasks....");
            _count.Wait();
            Console.WriteLine("all task completed");


            Console.ReadKey();
        }
      
    }

使用TASK的waitAll可以达到同样的效果:

 var t1 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(2000);
                Console.WriteLine("thread 1 complete");
            
            });
           var t2 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(5000);
                Console.WriteLine("thread 2 complete");
            
            });
           var t3 = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(3000);
                Console.WriteLine("thread 3 complete");
             
            });
            Console.WriteLine("waiting tasks....");
            Task.WaitAll(t1, t2, t3);
            Console.WriteLine("all task completed");


            Console.ReadKey();
Copy after login

The above is the content of the use of CountDownEvent in c#. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template