// <summary> // Calls the given continuation, after the given task completes, if it ends in a faulted or canceled state. // Will not be called if the task did not fault or cancel (meaning, it will not be called if the task ran // to completion). Intended to roughly emulate C# 5's support for "try/catch" in // async methods. Note that this method allows you to return a Task, so that you can either return // a completed Task (indicating that you swallowed the exception) or a faulted task (indicating that // that the exception should be propagated). In C#, you cannot normally use await within a catch // block, so returning a real async task should never be done from Catch(). // </summary> internal static Task <TResult> Catch <TResult>(this Task <TResult> task, Func <CatchInfo <TResult>, CatchInfo <TResult> .CatchResult> continuation, CancellationToken cancellationToken = default(CancellationToken)) { // Fast path for successful tasks, to prevent an extra TCS allocation if (task.Status == TaskStatus.RanToCompletion) { return(task); } return(task.CatchImpl(() => continuation(new CatchInfo <TResult>(task, cancellationToken)).Task, cancellationToken)); }
// <summary> // Calls the given continuation, after the given task completes, if it ends in a faulted or canceled state. // Will not be called if the task did not fault or cancel (meaning, it will not be called if the task ran // to completion). Intended to roughly emulate C# 5's support for "try/catch" in // async methods. Note that this method allows you to return a Task, so that you can either return // a completed Task (indicating that you swallowed the exception) or a faulted task (indicating that // that the exception should be propagated). In C#, you cannot normally use await within a catch // block, so returning a real async task should never be done from Catch(). // </summary> internal static Task Catch(this Task task, Func <CatchInfo, CatchInfoBase <Task> .CatchResult> continuation, CancellationToken cancellationToken = default(CancellationToken)) { // Fast path for successful tasks, to prevent an extra TCS allocation if (task.Status == TaskStatus.RanToCompletion) { return(task); } return(task.CatchImpl(() => continuation(new CatchInfo(task, cancellationToken)).Task.ToTask <AsyncVoid>(), cancellationToken)); }
public static Task <TResult> Catch <TResult>(this Task <TResult> task, Func <CatchInfo <TResult>, CatchInfo <TResult> .CatchResult> continuation, CancellationToken cancellationToken = default(CancellationToken)) { if (task.Status == TaskStatus.RanToCompletion) { return(task); } return(task.CatchImpl(() => continuation(new CatchInfo <TResult>(task, cancellationToken)).Task, cancellationToken)); }
/// <summary> /// Calls the given continuation, after the given task completes, if it ends in a faulted state. /// Will not be called if the task did not fault (meaning, it will not be called if the task ran /// to completion or was canceled). Intended to roughly emulate C# 5's support for "try/catch" in /// async methods. Note that this method allows you to return a Task, so that you can either return /// a completed Task (indicating that you swallowed the exception) or a faulted task (indicating that /// that the exception should be propagated). In C#, you cannot normally use await within a catch /// block, so returning a real async task should never be done from Catch(). /// </summary> internal static Task <TResult> Catch <TResult>(this Task <TResult> task, Func <CatchInfo <TResult>, CatchInfo <TResult> .CatchResult> continuation, CancellationToken cancellationToken = default(CancellationToken)) { return(task.CatchImpl(() => continuation(new CatchInfo <TResult>(task)).Task, cancellationToken)); }
/// <summary> /// Calls the given continuation, after the given task completes, if it ends in a faulted state. /// Will not be called if the task did not fault (meaning, it will not be called if the task ran /// to completion or was canceled). Intended to roughly emulate C# 5's support for "try/catch" in /// async methods. Note that this method allows you to return a Task, so that you can either return /// a completed Task (indicating that you swallowed the exception) or a faulted task (indicating that /// that the exception should be propagated). In C#, you cannot normally use await within a catch /// block, so returning a real async task should never be done from Catch(). /// </summary> internal static Task Catch(this Task task, Func <CatchInfo, CatchInfo.CatchResult> continuation, CancellationToken cancellationToken = default(CancellationToken)) { return(task.CatchImpl(() => continuation(new CatchInfo(task)).Task.ToTask <AsyncVoid>(), cancellationToken)); }