/// <summary> /// Starts the given Action when one task has not successfully ended /// </summary> /// <param name="that">The that.</param> /// <param name="action">The action to start.</param> /// <param name="target">The DispatcherBase to start the following action on.</param> /// <returns> /// The tasks. /// </returns> public static IEnumerable <CustomTask> WhenFailed(this IEnumerable <CustomTask> that, Action action, CustomTaskDispatcherBase target) { var hasFailed = false; var syncRoot = new object(); foreach (var task in that) { task.WhenFailed(() => { lock (syncRoot) { if (hasFailed) { return; } hasFailed = true; if (target == null) { action(); } else { target.Dispatch(action); } } }); } return(that); }
/// <summary> /// The given Action will be performed when the task succeeds. /// </summary> /// <param name="task">The task.</param> /// <param name="action">The action to perform.</param> /// <param name="actionTarget">The action target.</param> /// <returns> /// This task. /// </returns> public static CustomTask WhenSucceeded(this CustomTask task, Action <CustomTask> action, CustomTaskDispatcherBase actionTarget) { Action <CustomTask> perform = t => { if (actionTarget == null) { action(t); } else { actionTarget.Dispatch(() => { if (t.IsSucceeded) { action(t); } }); } }; return(task.WhenEnded(t => { if (t.IsSucceeded) { perform(t); } }, null)); }
/// <summary> /// Starts the given Action when all Tasks ended successfully. /// </summary> /// <param name="that">The that.</param> /// <param name="action">The action to start.</param> /// <param name="target">The DispatcherBase to start the following action on.</param> /// <returns> /// The tasks. /// </returns> public static IEnumerable <CustomTask> WhenSucceeded(this IEnumerable <CustomTask> that, Action action, CustomTaskDispatcherBase target) { var remaining = that.Count(); var syncRoot = new object(); foreach (var task in that) { task.WhenSucceeded(() => { lock (syncRoot) { remaining--; if (remaining != 0) { return; } if (target == null) { action(); } else { target.Dispatch(action); } } }); } return(that); }
/// <summary> /// The given Action will be performed when the task ends. /// </summary> /// <param name="task">The task.</param> /// <param name="action">The action to perform.</param> /// <param name="target">The DispatcherBase to perform the action on.</param> /// <returns> /// This task. /// </returns> public static CustomTask WhenEnded(this CustomTask task, Action <CustomTask> action, CustomTaskDispatcherBase target) { task.TaskEnded += (t) => { if (target == null) { action(task); } else { target.Dispatch(() => action(task)); } }; return(task); }