/// <summary> /// Propagates the completion state of <paramref name="task"/> (<see cref="TaskStatus.Canceled"/>, <see cref="TaskStatus.Faulted"/>, /// or <see cref="TaskStatus.RanToCompletion"/>) to the given <paramref name="taskCompletionSource"/>, optionally using /// <paramref name="resultFactory"/> to generate a <typeparamref name="TResult"/> value (otherwise the default <typeparamref name="TResult"/> /// value is used. Returns the input <paramref name="taskCompletionSource"/> /// </summary> public static TaskCompletionSource <TResult> CompleteWith <TResult>(this TaskCompletionSource <TResult> taskCompletionSource, Task task, Func <TResult> resultFactory = null) { var continuationOptions = resultFactory == null ? TaskContinuationOptions.ExecuteSynchronously : TaskContinuationOptions.None; return(taskCompletionSource.InternalCompleteWith(task, (_, state) => state != null ? ((Func <TResult>)state)() : default(TResult), resultFactoryState: resultFactory, continuationOptions: continuationOptions)); }
/// <summary> /// Propagates the completion state of <paramref name="task"/> (<see cref="TaskStatus.Canceled"/>, <see cref="TaskStatus.Faulted"/>, /// or <see cref="TaskStatus.RanToCompletion"/>) to the given <paramref name="taskCompletionSource"/>, using <paramref name="resultFactory"/> /// to generate a <typeparamref name="TResult"/> value from the given <paramref name="task"/>'s result. Returns the input /// <paramref name="taskCompletionSource"/> /// </summary> public static TaskCompletionSource <TResult> CompleteWith <TResult, TTaskResult>(this TaskCompletionSource <TResult> taskCompletionSource, Task <TTaskResult> task, Func <TTaskResult, TResult> resultFactory) { if (resultFactory == null) { throw new ArgumentNullException(nameof(resultFactory)); } return(taskCompletionSource.InternalCompleteWith(task, (t, state) => ((Func <TTaskResult, TResult>)state)(t.Result), resultFactoryState: resultFactory)); }
/// <summary> /// Propagates the completion state of <paramref name="task"/> (<see cref="TaskStatus.Canceled"/>, <see cref="TaskStatus.Faulted"/>, /// or <see cref="TaskStatus.RanToCompletion"/>) to the given <paramref name="taskCompletionSource"/>. Returns the input /// <paramref name="taskCompletionSource"/> /// </summary> public static TaskCompletionSource <TResult> CompleteWith <TResult>(this TaskCompletionSource <TResult> taskCompletionSource, Task <TResult> task) { return(taskCompletionSource.InternalCompleteWith(task, (t, _) => t.Result, resultFactoryState: null, continuationOptions: TaskContinuationOptions.ExecuteSynchronously)); }