1. 程式人生 > 其它 >C# 多執行緒等待子執行緒全部完成 ThreadPool

C# 多執行緒等待子執行緒全部完成 ThreadPool

 private void button1_Click(object sender, EventArgs e)
        { 
            
            //假設有10個請求執行緒
            int num = 10;
            using (MultiThreadResetEvent multiThreadResetEvent = new MultiThreadResetEvent(num))
            {
                for (int i = 0; i < num; i++)
                {
                    //ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                    Param pra = new Param();
                    pra.multiThreadResetEvent = multiThreadResetEvent;
                    pra.value = i;
                    //manualResetEvents.Add(manualResetEvent);
                    ThreadPool.QueueUserWorkItem(Call, pra);

                }
                //WaitHandle.WaitAll(manualResetEvents.ToArray());

                multiThreadResetEvent.WaitAll();
            }
       
            Console.WriteLine("main thread is success");
        }

  

  private void Call(object state)
        {
            Thread.Sleep(1000);
            Param param = state as Param;


            Console.WriteLine($"Thread execute {param.value} --- {DateTime.Now}  ");
            param.multiThreadResetEvent.SetOne(); 
        }

  

    public  class Param
    {
        public Param()
        {
        }

        public MultiThreadResetEvent multiThreadResetEvent { get;  set; }
        public int value { get;  set; }
    }

  

  /// <summary>
    /// 多執行緒訊號等待
    /// </summary>
    public class MultiThreadResetEvent : IDisposable
    {
        private readonly ManualResetEvent done;
        private readonly int total;
        private long current;

        /// <summary>
        /// 監視total個執行緒執行(執行緒數固定,可以超過64個)
        /// </summary>
        /// <param name="total">需要等待執行的執行緒總數</param>
        public MultiThreadResetEvent(int total)
        {
            this.total = total;
            current = total;
            done = new ManualResetEvent(false);
        }

        /// <summary>
        /// 執行緒數不固定,監視任意執行緒數時
        /// </summary>
        public MultiThreadResetEvent()
        {
            done = new ManualResetEvent(false);
        }

        /// <summary>
        /// 加入一個要等待的執行緒訊號
        /// </summary>
        public void addWaitOne()
        {
            Interlocked.Increment(ref current);
        }
        /// <summary>
        /// 喚醒一個等待的執行緒
        /// </summary>
        public void SetOne()
        {
            // Interlocked 原子操作類 ,此處將計數器減1
            if (Interlocked.Decrement(ref current) == 0)
            {
                //當所以等待執行緒執行完畢時,喚醒等待的執行緒
                done.Set();
            }
        }
        /// <summary>
        /// 等待所以執行緒執行完畢
        /// </summary>
        public void WaitAll()
        {
            done.WaitOne();
        }
        /// <summary>
        /// 釋放物件佔用的空間
        /// </summary>
        public void Dispose()
        {
            ((IDisposable)done).Dispose();
        }
    }

  測試

作者:xyyhqq 出處:https://www.cnblogs.com/xyyhcn/

-------------------------------------------

Never Stop !