Created
February 1, 2017 02:52
-
-
Save brsmith080/9cf880fa64561b3b6bdc5550a99bc1d3 to your computer and use it in GitHub Desktop.
continue to task
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<TNewResult> ContinueWithTask<TContinue, TNewResult>(this Task<TContinue> task, Func<Task<TContinue>, Task<TNewResult>> Continuation, CancellationToken CancellationToken) | |
{ | |
var Completion = new TaskCompletionSource<TNewResult>(); | |
task.ContinueWith(t => | |
{ | |
if (CancellationToken.IsCancellationRequested) | |
{ | |
Completion.SetCanceled(); | |
return; | |
} | |
if (t.IsFaulted) | |
{ | |
Completion.TrySetException(t.Exception); | |
return; | |
} | |
Continuation(t).ContinueWith(t2 => | |
{ | |
if (CancellationToken.IsCancellationRequested || t2.IsCanceled) Completion.TrySetCanceled(); | |
else if (t2.IsFaulted) Completion.TrySetException(t2.Exception); | |
else Completion.SetResult(t2.Result); | |
}); | |
}); | |
return Completion.Task; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment