/// <summary> /// Starts the given Task when the tasks ended successfully. /// </summary> /// <param name="that">The that.</param> /// <param name="followingTask">The task to start.</param> /// <param name="target">The DispatcherBase to start the following task on.</param> /// <returns> /// The tasks. /// </returns> public static IEnumerable <CustomTask> Then(this IEnumerable <CustomTask> that, CustomTask followingTask, CustomTaskDispatcherBase target) { var remaining = that.Count(); var syncRoot = new object(); foreach (var task in that) { task.WhenFailed(() => { if (followingTask.ShouldAbort) { return; } followingTask.Abort(); }); task.WhenSucceeded(() => { if (followingTask.ShouldAbort) { return; } lock (syncRoot) { remaining--; if (remaining != 0) { return; } if (target != null) { followingTask.Run(target); } else if (CustomThread.CurrentThread is TaskWorker) { followingTask.Run(((TaskWorker)CustomThread.CurrentThread).TaskDistributor); } else { followingTask.Run(); } } }); } return(that); }