|
| 1 | +// --------------------------------------------------------------------------------------------------------------------- |
| 2 | +// Imports |
| 3 | +// --------------------------------------------------------------------------------------------------------------------- |
| 4 | +namespace CodeOfChaos.Extensions; |
| 5 | +// --------------------------------------------------------------------------------------------------------------------- |
| 6 | +// Code |
| 7 | +// --------------------------------------------------------------------------------------------------------------------- |
| 8 | +public static class TaskWhenAllHelper { |
| 9 | + |
| 10 | + // ValueTask because in theory tasks can be null and thus we quite without the need for a task |
| 11 | + private static async ValueTask WhenAllWrapper(params Task[] tasks) { |
| 12 | + int taskCount = tasks.Length; |
| 13 | + |
| 14 | + // Do some checks before we run the tasks |
| 15 | + for (int i = 0; i < taskCount; i++) { |
| 16 | + Task task = tasks[i]; |
| 17 | + ArgumentNullException.ThrowIfNull(task); |
| 18 | + } |
| 19 | + |
| 20 | + await Task.WhenAll(tasks); |
| 21 | + |
| 22 | + // Check if the tasks have successfully run |
| 23 | + var exceptionsToThrow = new List<Exception>(); |
| 24 | + for (int i = 0; i < taskCount; i++) { |
| 25 | + switch (tasks[i]) { |
| 26 | + case { IsCanceled: true } task: { |
| 27 | + exceptionsToThrow.Add(new TaskCanceledException(task)); |
| 28 | + break; |
| 29 | + } |
| 30 | + |
| 31 | + case { IsFaulted: true } task: { |
| 32 | + exceptionsToThrow.Add(task.Exception); |
| 33 | + break; |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (exceptionsToThrow.Count != 0) throw new AggregateException(exceptionsToThrow); |
| 39 | + } |
| 40 | + |
| 41 | + public static async Task<(T0, T1)> WhenAll<T0, T1>(Task<T0> task0, Task<T1> task1) { |
| 42 | + await WhenAllWrapper(task0, task1); |
| 43 | + return (task0.Result, task1.Result); |
| 44 | + } |
| 45 | + |
| 46 | + public static async Task<(T0, T1, T2)> WhenAll<T0, T1, T2>(Task<T0> task0, Task<T1> task1, Task<T2> task2) { |
| 47 | + await WhenAllWrapper(task0, task1, task2); |
| 48 | + return (task0.Result, task1.Result, task2.Result); |
| 49 | + } |
| 50 | + |
| 51 | + public static async Task<(T0, T1, T2, T3)> WhenAll<T0, T1, T2, T3>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3) { |
| 52 | + await WhenAllWrapper(task0, task1, task2, task3); |
| 53 | + return (task0.Result, task1.Result, task2.Result, task3.Result); |
| 54 | + } |
| 55 | + |
| 56 | + public static async Task<(T0, T1, T2, T3, T4)> WhenAll<T0, T1, T2, T3, T4>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4) { |
| 57 | + await WhenAllWrapper(task0, task1, task2, task3, task4); |
| 58 | + return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result); |
| 59 | + } |
| 60 | + |
| 61 | + public static async Task<(T0, T1, T2, T3, T4, T5)> WhenAll<T0, T1, T2, T3, T4, T5>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5) { |
| 62 | + await WhenAllWrapper(task0, task1, task2, task3, task4, task5); |
| 63 | + return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result, task5.Result); |
| 64 | + } |
| 65 | + |
| 66 | + public static async Task<(T0, T1, T2, T3, T4, T5, T6)> WhenAll<T0, T1, T2, T3, T4, T5, T6>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5, Task<T6> task6) { |
| 67 | + await WhenAllWrapper(task0, task1, task2, task3, task4, task5, task6); |
| 68 | + return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result, task5.Result, task6.Result); |
| 69 | + } |
| 70 | + |
| 71 | + public static async Task<(T0, T1, T2, T3, T4, T5, T6, T7)> WhenAll<T0, T1, T2, T3, T4, T5, T6, T7>(Task<T0> task0, Task<T1> task1, Task<T2> task2, Task<T3> task3, Task<T4> task4, Task<T5> task5, Task<T6> task6, Task<T7> task7) { |
| 72 | + await WhenAllWrapper(task0, task1, task2, task3, task4, task5, task6, task7); |
| 73 | + return (task0.Result, task1.Result, task2.Result, task3.Result, task4.Result, task5.Result, task6.Result, task7.Result); |
| 74 | + } |
| 75 | +} |
0 commit comments