示例#1
0
        /// <summary>
        /// Starts the given Task when the tasks ended successfully.
        /// </summary>
        /// <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 <Task> Then(this IEnumerable <Task> that, Task followingTask, DispatcherBase 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)
                        {
                            if (target != null)
                            {
                                followingTask.Run(target);
                            }
                            else if (ThreadBase.CurrentThread is TaskWorker)
                            {
                                followingTask.Run(((TaskWorker)ThreadBase.CurrentThread).TaskDistributor);
                            }
                            else
                            {
                                followingTask.Run();
                            }
                        }
                    }
                });
            }
            return(that);
        }
 /// <summary>
 /// Starts the given Task when this Task ended successfully.
 /// </summary>
 /// <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 Task Then(this Task that, Task followingTask, DispatcherBase target)
 {
     that.WhenFailed(() =>
     {
         followingTask.Abort();
     });
     that.WhenSucceeded(() =>
     {
         if (target != null)
         {
             followingTask.Run(target);
         }
         else if (ThreadBase.CurrentThread is TaskWorker)
         {
             followingTask.Run(((TaskWorker)ThreadBase.CurrentThread).TaskDistributor);
         }
         else
         {
             followingTask.Run();
         }
     });
     return(that);
 }