If you need to wait until all tasks are processed, you will have to process it yourself. ThreadPool threads are background threads and will not keep the application operational.
This is a relatively clean way to handle this situation:
using (var mre = new ManualResetEvent(false)) { int remainingToProcess = workItems.Count(); // Assuming workItems is a collection of "tasks" foreach(var item in workItems) { // Delegate closure (in C
As they say, it is usually better to group your work items if you have thousands of them and not consider them as separate work items for threadpool. These are some of the overhead of managing a list of items, and since you wonโt be able to process 22,000 at a time, you'd better group them into blocks. With separate work items, each 50 or so process is likely to help your overall throughput quite a bit ...
Reed copsey
source share