src/NAsync/TaskExtensions.cs
/// <summary>
/// Remark: Bubble up a ArgumentNullException If next() returned null.
/// </summary>
Bubble up a ArgumentNullException If next() returned null.
Bubble up a ArgumentNullException If next () returned null .
[NotNull]
        public static Task Then<T1>([NotNull] this Task<T1> first,
            [NotNull] Func<T1, Task> next,
            CancellationToken cancellationToken,
            [CanBeNull] TaskScheduler taskScheduler = null)
        {
            if (first == null) throw new ArgumentNullException("first");
            if (next == null) throw new ArgumentNullException("next");
            TakeOverResponsabilityForUnobservedException(first);
            var tcs = new TaskCompletionSource<object>();
            first.ContinueWith(delegate
            {
                if (cancellationToken.IsCancellationRequested) tcs.TrySetCanceled();
                else if (first.IsFaulted) tcs.TrySetException(first.Exception.InnerExceptions);
                else if (first.IsCanceled) tcs.TrySetCanceled();
                else
                {
                    try
                    {
                        Task t = next(first.Result);
                        if (t == null) tcs.TrySetException(new ArgumentNullException("next", "Then(Func<T1, Task> nextTask) returned null. Not allowed"));
                        else
                            t.ContinueWith(delegate
                            {
                                if (t.IsFaulted) tcs.TrySetException(t.Exception.InnerExceptions);
                                else if (t.IsCanceled) tcs.TrySetCanceled();
                                else tcs.TrySetResult(null);
                            },
                                taskScheduler ?? GetDefaultTaskScheduler());
                    }
                    catch (Exception exc)
                    {
                        tcs.TrySetException(exc);
                    }
                }
            },
                taskScheduler ?? GetDefaultTaskScheduler());
            return tcs.Task;
        }
[ NotNull ] public static Task Then < T1 > ( [ NotNull ] this Task < T1 > first , [ NotNull ] Func < T1 , Task > next , CancellationToken cancellationToken , [ CanBeNull ] TaskScheduler taskScheduler = null ) { if ( first == null ) throw new ArgumentNullException ( " " ) ; if ( next == null ) throw new ArgumentNullException ( " " ) ; TakeOverResponsabilityForUnobservedException ( first ) ; var tcs = new TaskCompletionSource < object > ( ) ; first . ContinueWith ( delegate { if ( cancellationToken . IsCancellationRequested ) tcs . TrySetCanceled ( ) ; else if ( first . IsFaulted ) tcs . TrySetException ( first . Exception . InnerExceptions ) ; else if ( first . IsCanceled ) tcs . TrySetCanceled ( ) ; else { try { Task t = next ( first . Result ) ; if ( t == null ) tcs . TrySetException ( new ArgumentNullException ( " " , " " ) ) ; else t . ContinueWith ( delegate { if ( t . IsFaulted ) tcs . TrySetException ( t . Exception . InnerExceptions ) ; else if ( t . IsCanceled ) tcs . TrySetCanceled ( ) ; else tcs . TrySetResult ( null ) ; } , taskScheduler ?? GetDefaultTaskScheduler ( ) ) ; } catch ( Exception exc ) { tcs . TrySetException ( exc ) ; } } } , taskScheduler ?? GetDefaultTaskScheduler ( ) ) ; return tcs . Task ; }
Remark: Bubble up a ArgumentNullException If next() returned null.
Remark : Bubble up a ArgumentNullException If next () returned null .
// ReSharper disable once PossibleNullReferenceException // ReSharper disable PossibleNullReferenceException // ReSharper restore PossibleNullReferenceException
this
[NotNull]
Func<T1, Task>
[NotNull]
cancellationToken
CancellationToken
TaskScheduler
[CanBeNull]