src/NAsync/TaskExtensions.cs
/// <summary> 
/// Allow to do some stuff if the first task has been cancelled.
/// Propagates the cancellation to the resulting task, or the exception of the callback if it threw an exception
/// </summary>
Allow to do some stuff if the first task has been cancelled.
Propagates the cancellation to the resulting task, or the exception of the callback if it threw an exception
Allow to do some stuff if the first task has been cancelled . Propagates the cancellation to the resulting task or the exception of the callback if it threw an exception
[NotNull]
        public static Task OnCancelled([NotNull] this Task first,
            [NotNull] Action next,
            [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 (first.IsFaulted) tcs.TrySetException(first.Exception.InnerExceptions);
                else if (first.IsCanceled)
                {
                    try
                    {
                        next();
                        tcs.SetCanceled();
                    }
                    catch(Exception e)
                    {
                        tcs.TrySetException(e); 
                    }
                }
                else tcs.SetResult(null); 
            },
                taskScheduler ?? GetDefaultTaskScheduler());
            return tcs.Task;
        }
[ NotNull ] public static Task OnCancelled ( [ NotNull ] this Task first , [ NotNull ] Action next , [ 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 ( first . IsFaulted ) tcs . TrySetException ( first . Exception . InnerExceptions ) ; else if ( first . IsCanceled ) { try { next ( ) ; tcs . SetCanceled ( ) ; } catch ( Exception e ) { tcs . TrySetException ( e ) ; } } else tcs . SetResult ( null ) ; } , taskScheduler ?? GetDefaultTaskScheduler ( ) ) ; return tcs . Task ; }
Allow to do some stuff if the first task has been cancelled.
Allow to do some stuff if the first task has been cancelled .
// ReSharper disable once PossibleNullReferenceException // forward
this
[NotNull]
Action
[NotNull]
TaskScheduler
[CanBeNull]