Workaround for the WaitHandle.WaitAll 64 handle limit?

前端 未结 9 1934
孤独总比滥情好
孤独总比滥情好 2020-11-28 04:19

My application spawns loads of different small worker threads via ThreadPool.QueueUserWorkItem which I keep track of via multiple ManualResetEvent

9条回答
  •  不知归路
    2020-11-28 05:08

    Here is another solution. Here is the "events" is a list of ManualResetEvent. The size of the list can be greater than 64 (MAX_EVENTS_NO).

    int len = events.Count;
    if (len <= MAX_EVENTS_NO)
        {
            WaitHandle.WaitAll(events.ToArray());
        } else {
            int start = 0;
            int num = MAX_EVENTS_NO;
            while (true)
                {
                    if(start + num > len)
                    {
                       num = len - start;
                    }
                    List sublist = events.GetRange(start, num);
                    WaitHandle.WaitAll(sublist.ToArray());
                    start += num;
                    if (start >= len)
                       break;
               }
       }
    
    

提交回复
热议问题