/// <summary> /// Starts the given Action when all Tasks ended successfully. /// </summary> /// <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 <Task> WhenSucceeded(this IEnumerable <Task> that, Action action, DispatcherBase target) { var remaining = that.Count(); var syncRoot = new object(); foreach (var task in that) { task.WhenSucceeded(() => { lock (syncRoot) { remaining--; if (remaining == 0) { if (target == null) { action(); } else { target.Dispatch(() => { action(); }); } } } }); } return(that); }
/// <summary> /// Starts the given Action when one task has not successfully ended /// </summary> /// <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 <Task> WhenFailed(this IEnumerable <Task> that, Action action, DispatcherBase 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> /// Starts the task on the given DispatcherBase (Dispatcher or TaskDistributor). /// </summary> /// <param name="target">The DispatcherBase to work on.</param> /// <returns>This task</returns> public Task Run(DispatcherBase target) { if (target == null) { return(Run()); } target.Dispatch(this); return(this); }
/// <summary> /// The given Action will be performed when the task ends. /// </summary> /// <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 Task WhenEnded(this Task task, Action <Task> action, DispatcherBase target) { task.TaskEnded += (t) => { if (target == null) { action(task); } else { target.Dispatch(() => action(task)); } }; return(task); }
/// <summary> /// The given Action will be performed when the task succeeds. /// </summary> /// <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 Task WhenSucceeded(this Task task, Action <Task> action, DispatcherBase actiontargetTarget) { Action <Task> perform = t => { if (actiontargetTarget == null) { action(t); } else { actiontargetTarget.Dispatch(() => { if (t.IsSucceeded) { action(t); } }); } }; return(task.WhenEnded(t => { if (t.IsSucceeded) { perform(t); } }, null)); }
/// <summary> /// The given Action will be performed when the task succeeds. /// </summary> /// <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 Task <T> WhenSucceeded <T>(this Task <T> task, Action <Task <T> > action, DispatcherBase target) { Action <Task <T> > perform = t => { if (target == null) { action(t); } else { target.Dispatch(() => { if (t.IsSucceeded) { action(t); } }); } }; return(task.WhenEnded <T>(t => { if (t.IsSucceeded) { perform(t); } }, null)); }