[英]I have a thread.pool and how can i make Main Thread wait until all task are completed?
嗨,我有下面的代碼,我想知道如何讓主線程等待線程池中的所有任務完成(我有動態的任務數量,並且最大分配了3個CPU內核)。 例如:我有5個作業來運行thread_pool最多可以並行執行3個。
{
for(i=0;i<n;i++)
{
TaskParams tp = new TaskParams(some params to be parsed to thread job);
ScanAllSectors objAllSectors = new ScanAllSectors();//object containing job function
ThreadPool.QueueUserWorkItem(objAllSectors.vScan, tp);
}
//i want to block it here
...//some other functions that uses the result from the code above`
}
...所以這就是為什么我要阻止main_thread直到線程池中的所有作業都完成,然后才能運行以下功能。
謝謝!
Parallel.ForEach(
ListOfStuff,
new ParallelOptions { MaxDegreeOfParallelism = 4 },
myStuffObj => DoAwesomeStuff(myStuffObj)
);
Console.WriteLine("All stuff done);
進一步閱讀
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.