1. 程式人生 > >信號量 AutoResetEvent與WaitHandle.WaitAll使用。

信號量 AutoResetEvent與WaitHandle.WaitAll使用。

void edt linq cati length read sin bsp string

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();//設置為有信號。 } } }

信號量 AutoResetEvent與WaitHandle.WaitAll使用。