/// <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> /// 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); }
/// <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); }
/// <summary> /// The given Action will be performed when the task fails. /// </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 WhenFailed(this CustomTask task, Action <CustomTask> action, CustomTaskDispatcherBase target) { return(task.WhenEnded(t => { if (t.IsFailed) { action(t); } }, target)); }
/// <summary> /// Starts this Task when the other Task ended successfully. /// </summary> /// <param name="that">The that.</param> /// <param name="taskToWaitFor">The task to wait for.</param> /// <param name="target">The DispatcherBase to start this task on.</param> /// <returns> /// This task. /// </returns> public static CustomTask Await(this CustomTask that, CustomTask taskToWaitFor, CustomTaskDispatcherBase target) { taskToWaitFor.Then(that, target); return(that); }
/// <summary> /// Starts the given Task when this Task 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> /// This task. /// </returns> public static CustomTask Then(this CustomTask that, CustomTask followingTask, CustomTaskDispatcherBase target) { that.WhenFailed(followingTask.Abort); that.WhenSucceeded(() => { if (target != null) { followingTask.Run(target); } else if (CustomThread.CurrentThread is TaskWorker) { followingTask.Run(((TaskWorker)CustomThread.CurrentThread).TaskDistributor); } else { followingTask.Run(); } }); return(that); }
/// <summary> /// Invokes the given action with the set result of the task when the task succeeded. /// </summary> /// <typeparam name="T"></typeparam> /// <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 <T> OnResult <T>(this CustomTask <T> task, Action <T> action, CustomTaskDispatcherBase actionTarget) { return(task.WhenSucceeded(t => action(t.Result), actionTarget)); }
/// <summary> /// Invokes the given action with the set result of the task when the task succeeded. /// </summary> /// <typeparam name="T"></typeparam> /// <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 OnResult <T>(this CustomTask task, Action <T> action, CustomTaskDispatcherBase target) { return(task.WhenSucceeded(t => action((T)t.RawResult), target)); }