Created
January 2, 2023 22:53
-
-
Save kevingosse/38fee834dfec21c9e20dce414f469117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Task WhenAllOrFaulted(params Task[] tasks) | |
{ | |
var faultedContinuations = new Task[tasks.Length]; | |
var faultedTask = new TaskCompletionSource<Task>(); | |
for (int i = 0; i < tasks.Length; i++) | |
{ | |
faultedContinuations[i] = tasks[i].ContinueWith(t => faultedTask.SetResult(t), TaskContinuationOptions.OnlyOnFaulted); | |
} | |
var successTask = Task.WhenAll(tasks); | |
return Task.WhenAny(faultedTask.Task, successTask); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment