我希望使Task.WaitAll()在任何正在运行的任务抛出异常时中止,以便我不必等待60秒钟才能完成。我如何实现这样的行为? 如果WaitAll()无法实现该功能,是否有其他C#特性或解决方法?
Task task1 = Task.Run(() => throw new InvalidOperationException());
Task task2 = ...
...
try
{
Task.WaitAll(new Task[]{task1, task2, ...}, TimeSpan.FromSeconds(60));
}
catch (AggregateException)
{
// If any exception thrown on any of the tasks, break out immediately instead of wait all the way to 60 seconds.
}