If I add jobs to the thread pool with QueueUserWorkItem
... how do I keep my program from going forward until all jobs are completed?
I know I could add some logic to keep the app from running until all jobs are completed, but I want to know if there is something like Thread.Join()
or if there's any way to retrieve each thread that is being assigned a job.
The best way to do this is to use the
CountdownEvent
class. This is a fairly well established pattern and is about as scalable as it gets.You can use .NET's Barrier class to achieve this.
You could use events to sync. Like this:
If you don't want to embed event set into your method, you could do something like this:
For multiple items: