信号量 AutoResetEvent与WaitHandle.WaitAll使用。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

namespace ConsoleApplication11

{

    class Program

    {

     

        static void Main(string[] args)

        {

            AutoResetEvent[] AutoR = new AutoResetEvent[5];

            for (int i = 0; i < AutoR.Length; i++)

            {

                AutoR[i] = new AutoResetEvent(false);

                Thread td = new Thread(new ParameterizedThreadStart(ok));

                td.IsBackground = true;

                td.Start(AutoR[i]);

            }

            WaitHandle.WaitAll(AutoR);

            Console.WriteLine("全部收到信息,完成任务");

        }

        static void ok(object i)

        {

            AutoResetEvent reset = (AutoResetEvent)i;

            Console.WriteLine("程序往下执行。");

            Thread.Sleep(10000);

            reset.Set();//设置为有信号。

        }

    }
} 
原文地址:https://www.cnblogs.com/larva/p/7222341.html