如何确保在 TPL 完成所有任务时触发回调

How to ensure a callback will fire when all task done by TPL

告诉我如何编写代码,结果将调用回调函数通知我所有任务(如 task1、task2、task3)已完成。谢谢

您可以将 Task.ContinueWithTask.WhenAll 一起使用,也可以将 Task.WaitAll 与调用 WaitAll 后的代码 运行 一起使用。

var executingTask = Task.WhenAll(task1, task2, task3).ContinueWith((antecedent) =>{/*your code*/});

有关更多详细信息,请参阅 Task.ContinueWith documentation

// WaitAll blocks until all tasks are complete
Task.WaitAll(task1, task2, task3);
/*your code on the following lines(s) which will run after task1,task2,task3 are complete*/