Ejemplo n.º 1
0
 internal void SetFaultHandler(TaskBase handler)
 {
     Task.ContinueWith(t => handler.Start(t), Token,
                       TaskContinuationOptions.OnlyOnFaulted,
                       TaskManager.GetScheduler(handler.Affinity));
     DependsOn?.SetFaultHandler(handler);
 }
Ejemplo n.º 2
0
 public ITask Finally <T>(T taskToContinueWith)
     where T : ITask
 {
     Guard.ArgumentNotNull(taskToContinueWith, nameof(taskToContinueWith));
     continuationOnAlways = (TaskBase)(object)taskToContinueWith;
     continuationOnAlways.SetDependsOn(this);
     DependsOn?.SetFaultHandler(continuationOnAlways);
     return(continuationOnAlways);
 }
Ejemplo n.º 3
0
 internal virtual ITask Finally <T>(T taskToContinueWith)
     where T : TaskBase
 {
     Guard.ArgumentNotNull(taskToContinueWith, nameof(taskToContinueWith));
     continuation       = (TaskBase)(object)taskToContinueWith;
     continuationAlways = true;
     continuation.SetDependsOn(this);
     DependsOn?.SetFaultHandler((TaskBase)(object)continuation);
     return(continuation);
 }
Ejemplo n.º 4
0
 internal virtual ITask Finally <T>(T continuation)
     where T : ITask
 {
     Guard.ArgumentNotNull(continuation, "continuation");
     continuation.SetDependsOn(this);
     this.continuation       = (TaskBase)(object)continuation;
     this.continuationAlways = true;
     DependsOn?.SetFaultHandler((TaskBase)(object)continuation);
     return(continuation);
 }
Ejemplo n.º 5
0
        public ITask Finally(Action <bool, Exception> actionToContinueWith, TaskAffinity affinity = TaskAffinity.Concurrent)
        {
            Guard.ArgumentNotNull(actionToContinueWith, nameof(actionToContinueWith));
            var ret = Then(new ActionTask(Token, actionToContinueWith)
            {
                Affinity = affinity, Name = "Finally"
            }, true);

            DependsOn?.SetFaultHandler(ret);
            ret.ContinuationIsFinally = true;
            return(ret);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// This does not set a dependency between the two tasks. Instead,
 /// the Start method grabs the state of the previous task to pass on
 /// to the next task via previousSuccess and previousException
 /// </summary>
 /// <param name="handler"></param>
 internal void SetFaultHandler(TaskBase handler)
 {
     Task.ContinueWith(t =>
     {
         Token.ThrowIfCancellationRequested();
         handler.Start(t);
     },
                       Token,
                       TaskContinuationOptions.OnlyOnFaulted,
                       TaskManager.GetScheduler(handler.Affinity));
     DependsOn?.SetFaultHandler(handler);
 }
Ejemplo n.º 7
0
        public ITask Finally(Action <bool, Exception> continuation, TaskAffinity affinity = TaskAffinity.Concurrent)
        {
            Guard.ArgumentNotNull(continuation, "continuation");
            var ret = new ActionTask(Token, continuation, this, true)
            {
                Affinity = affinity, Name = "Finally"
            };

            DependsOn?.SetFaultHandler(ret);
            ret.ContinuationIsFinally = true;
            return(ret);
        }
Ejemplo n.º 8
0
        public virtual T Then <T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.OnSuccess)
            where T : ITask
        {
            Guard.ArgumentNotNull(nextTask, nameof(nextTask));
            var nextTaskBase = ((TaskBase)(object)nextTask);

            // find the task at the top of the chain
            nextTaskBase = nextTaskBase.GetTopMostTask() ?? nextTaskBase;
            // make the next task dependent on this one so it can get values from us
            nextTaskBase.SetDependsOn(this);

            if (runOptions == TaskRunOptions.OnSuccess)
            {
                this.continuationOnSuccess = nextTaskBase;

                // if there are fault handlers in the chain we're appending, propagate them
                // up this chain as well
                if (nextTaskBase.continuationOnFailure != null)
                {
                    SetFaultHandler(nextTaskBase.continuationOnFailure);
                }
                else if (nextTaskBase.continuationOnAlways != null)
                {
                    SetFaultHandler(nextTaskBase.continuationOnAlways);
                }
                if (nextTaskBase.catchHandler != null)
                {
                    Catch(nextTaskBase.catchHandler);
                }
                if (nextTaskBase.finallyHandler != null)
                {
                    Finally(nextTaskBase.finallyHandler);
                }
            }
            else if (runOptions == TaskRunOptions.OnFailure)
            {
                this.continuationOnFailure = nextTaskBase;
                DependsOn?.SetFaultHandler(nextTaskBase);
            }
            else
            {
                this.continuationOnAlways = nextTaskBase;
                DependsOn?.SetFaultHandler(nextTaskBase);
            }
            return(nextTask);
        }
Ejemplo n.º 9
0
        public virtual T Then <T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.OnSuccess, bool taskIsTopOfChain = false)
            where T : ITask
        {
            Guard.ArgumentNotNull(nextTask, nameof(nextTask));
            var nextTaskBase = ((TaskBase)(object)nextTask);

            // find the task at the top of the chain
            if (!taskIsTopOfChain)
            {
                nextTaskBase = nextTaskBase.GetTopMostTask() ?? nextTaskBase;
            }
            // make the next task dependent on this one so it can get values from us
            nextTaskBase.SetDependsOn(this);
            var nextTaskFinallyHandler = nextTaskBase.finallyHandler;

            if (runOptions == TaskRunOptions.OnSuccess)
            {
                this.continuationOnSuccess = nextTaskBase;

                // if there are fault handlers in the chain we're appending, propagate them
                // up this chain as well
                if (nextTaskBase.continuationOnFailure != null)
                {
                    SetFaultHandler(nextTaskBase.continuationOnFailure);
                }
                else if (nextTaskBase.continuationOnAlways != null)
                {
                    SetFaultHandler(nextTaskBase.continuationOnAlways);
                }
                if (nextTaskBase.catchHandler != null)
                {
                    Catch(nextTaskBase.catchHandler);
                }
                if (nextTaskFinallyHandler != null)
                {
                    Finally(nextTaskFinallyHandler);
                }
            }
            else if (runOptions == TaskRunOptions.OnFailure)
            {
                this.continuationOnFailure = nextTaskBase;
                DependsOn?.Then(nextTaskBase, TaskRunOptions.OnFailure, true);
            }
            else
            {
                this.continuationOnAlways = nextTaskBase;
                DependsOn?.SetFaultHandler(nextTaskBase);
            }

            // if the current task has a fault handler, attach it to the chain we're appending
            if (finallyHandler != null)
            {
                TaskBase endOfChainTask = (TaskBase)nextTaskBase.GetEndOfChain();
                while (endOfChainTask != this && endOfChainTask != null)
                {
                    endOfChainTask.finallyHandler += finallyHandler;
                    endOfChainTask = endOfChainTask.DependsOn;
                }
            }

            return(nextTask);
        }