I have a ConcurrentQueue that is being populated with an arbitrary number of objects, and I want to process those objects in separate threads.
How do I wait for all queued work items to be finished? The examples I’ve seen use a fixed array of ManualResetEvents, and then WaitHandle.WaitAll for them all to complete.
Do I need to manage the number of threads? How can I just keep queueing them up, and let the ThreadPool handle how many are running? There will be many tens of thousands of objects in the queue.
foreach (object A in myCollection)
{
ThreadPool.QueueUserWorkItem(A.process());
}
// now wait for all threads to finish
Or do I have to track all the ManualResetEvents in a list or something, and then WaitAll for all of them to report complete?
You should simply not queue work items but use the tasks library (System.Threading.Tasks) which gives you a lot more functionality around this, including a replacable scheduler.