Last active
August 29, 2015 14:11
-
-
Save patridge/c0720a4354b755662e11 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 class TaskHelper { | |
public static Task CompletedTask = AsCompletedTask(true); | |
public static Task<T> AsCompletedTask<T>(T result) { | |
TaskCompletionSource<T> precompletedSource = new TaskCompletionSource<T>(); | |
precompletedSource.SetResult(result); | |
return precompletedSource.Task; | |
} | |
public static void TrySetResultTask<TResult>(this TaskCompletionSource<TResult> tcs, Task<TResult> task) { | |
if (task == null) { | |
throw new ArgumentNullException("task"); | |
} | |
task.ContinueWith(t => { | |
tcs.TrySetResult(t.Result); | |
}); | |
} | |
public static void SetResultTask<TResult>(this TaskCompletionSource<TResult> tcs, Task<TResult> task) { | |
if (task == null) { | |
throw new ArgumentNullException("task"); | |
} | |
task.ContinueWith(t => { | |
tcs.SetResult(t.Result); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment