Skip to content

Instantly share code, notes, and snippets.

@patridge
Last active August 29, 2015 14:11
Show Gist options
  • Save patridge/c0720a4354b755662e11 to your computer and use it in GitHub Desktop.
Save patridge/c0720a4354b755662e11 to your computer and use it in GitHub Desktop.
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