Task.WaitAll doesn't wait for child task?

后端 未结 2 646
旧巷少年郎
旧巷少年郎 2020-12-11 06:51

Hello I have _noOfThreads as defined tasks to run at a time. So I keep continuing the Tasks by using % operator and at the end of the loop I have <

2条回答
  •  独厮守ぢ
    2020-12-11 07:35

    You are waiting only for the original task. To wait for all the continuations to complete, you need to call WaitAll on the continuation tasks. Simple way to accomplish this would be to reassign each continuation task back to the original variable so you are waiting only on the final continuations:

    else
        tasks[index % _noOfThreads] =
            tasks[index % _noOfThreads].ContinueWith(task => { foo.bar(); }, 
                            TaskContinuationOptions.AttachedToParent);
    

    See also this related question.

提交回复
热议问题