I have 30+ tasks that can be executed in parallel.
I use ThreadPool for each task.
But parent-function should not return until all tasks has completed.
I need a thread sync handle that would release WaitOne when its count reaches 0.
Something like:
foo.StartWith(myTasks.Count);
foreach (var task in myTasks) {
ThreadPool.QueueUserWorkItem(state => { task(state); foo.Release(); });
}
foo.WaitOne();
Semaphore feels right, just can’t figure out how to apply it here.
With C# 4.0 you can use the new CountdownEvent primitive.