Workaround for the WaitHandle.WaitAll 64 handle limit?

前端 未结 9 1926
孤独总比滥情好
孤独总比滥情好 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:06

    I did solved it by simply paginating the number of events to wait without much performace lost, and it's working perfectly on production environment. Follows the code:

            var events = new List();
    
            // code omited
    
            var newEvent = new ManualResetEvent(false);
            events.Add(newEvent);
            ThreadPool.QueueUserWorkItem(c => {
    
                //thread code
                newEvent.Set();
            });
    
            // code omited
    
            var wait = true;
            while (wait)
            {
                WaitHandle.WaitAll(events.Take(60).ToArray());
                events.RemoveRange(0, events.Count > 59 ? 60 : events.Count);
                wait = events.Any();
    
            }
    

提交回复
热议问题