Tag: 等待手柄

WaitHandle.WaitAll 64句柄限制的解决方法?

我的应用程序通过ThreadPool.QueueUserWorkItem产生不同的小工作者线程的负载,我通过多个ManualResetEvent实例跟踪。 我使用WaitHandle.WaitAll方法阻止我的应用程序closures,直到这些线程完成。 我从来没有任何问题,但是,因为我的应用程序正在更多的负载,即更多的线程被创build,我现在开始得到这个exception: WaitHandles must be less than or equal to 64 – missing documentation 什么是最好的替代解决scheme呢? 代码片段 List<AutoResetEvent> events = new List<AutoResetEvent>(); // multiple instances of… var evt = new AutoResetEvent(false); events.Add(evt); ThreadPool.QueueUserWorkItem(delegate { // do work evt.Set(); }); … WaitHandle.WaitAll(events.ToArray()); 解决方法 int threadCount = 0; ManualResetEvent finished = new ManualResetEvent(false); … Interlocked.Increment(ref threadCount); ThreadPool.QueueUserWorkItem(delegate […]