2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Threading.Tasks;
20 public static async ValueTask<T[]>
WhenAll<T>(IEnumerable<ValueTask<T>> tasks,
int totalTasks)
23 throw new ArgumentNullException(nameof(tasks));
28 var results =
new T[totalTasks];
29 foreach (var task
in tasks)
33 results[i] = await task.ConfigureAwait(
false);
43 Debug.Assert(i == totalTasks,
"Incorrect totalTasks specified!");
45 if (exception !=
null)
58 public static async ValueTask<T[]>
WhenAll<T>(IReadOnlyList<ValueTask<T>> tasks)
61 throw new ArgumentNullException(nameof(tasks));
63 var totalTasks = tasks.Count;
65 return Array.Empty<T>();
69 var results =
new T[totalTasks];
70 for (var i = 0; i < totalTasks; i++)
73 results[i] = await tasks[i].ConfigureAwait(
false);
80 if (exception !=
null)
92 public static ValueTask<T[]>
WhenAll<T>(params ValueTask<T>[] tasks) =>
WhenAll((IReadOnlyList<ValueTask<T>>)tasks);
99 public static async ValueTask
WhenAll(IEnumerable<ValueTask> tasks)
102 throw new ArgumentNullException(nameof(tasks));
106 foreach (var task
in tasks)
109 await task.ConfigureAwait(
false);
116 if (exception !=
null)
126 public static async ValueTask
WhenAll(IReadOnlyList<ValueTask> tasks)
129 throw new ArgumentNullException(nameof(tasks));
132 List<Exception>? exceptions =
null;
133 for (var i = 0; i < tasks.Count; ++i)
137 await task.ConfigureAwait(
false);
141 exceptions ??=
new(tasks.Count - i);
145 if (exceptions !=
null)
146 throw new AggregateException(exceptions);
155 public static ValueTask
WhenAll(params ValueTask[] tasks) =>
WhenAll((IReadOnlyList<ValueTask>)tasks);
Extension methods for the ValueTask and ValueTask<TResult> classes.
static ValueTask WhenAll(params ValueTask[] tasks)
Fully await a given list of tasks .
static async ValueTask WhenAll(IEnumerable< ValueTask > tasks)
Fully await a given list of tasks .
static async ValueTask WhenAll(IReadOnlyList< ValueTask > tasks)
Fully await a given list of tasks .
static async ValueTask< T[]> WhenAll< T >(IEnumerable< ValueTask< T > > tasks, int totalTasks)
Fully await a given list of tasks .