예제 #1
0
        public static SupercedableTaskState <TSource, TResult> CheckSupercede(SupercedableTaskState <TSource, TResult> current, TSource source, Func <TSource, CancellationToken, TResult> function)
        {
            if (current == null || current.ShouldBeSuperceded(source))
            {
                return(ForceSupercede(current, source, function));
            }

            return(current);
        }
예제 #2
0
        public static SupercedableTaskState <TSource, TResult> CheckSupercede(SupercedableTaskState <TSource, TResult> current, TSource source, TResult value)
        {
            if (current == null || current.ShouldBeSuperceded(source, value))
            {
                return(ForceSupercede(current, source, value));
            }

            return(current);
        }
예제 #3
0
        public void StartNew(TSource source, Func <TSource, CancellationToken, TResult> function)
        {
            if (function == null)
            {
                throw new ArgumentNullException("function");
            }

            using (SynchronizedStateChange <SupercedableTaskState <TSource, TResult> > stateChange = ChangeState())
                stateChange.NewState = SupercedableTaskState <TSource, TResult> .CheckSupercede(stateChange.CurrentState, source, function);
        }
예제 #4
0
        private static SupercedableTaskState <TSource, TResult> ForceSupercede(SupercedableTaskState <TSource, TResult> current, Func <SupercedableTaskState <TSource, TResult> > createFollowing)
        {
            SupercedableTaskState <TSource, TResult> following;

            try
            {
                if (current != null && !current.IsCompleted && !current.IsCancellationRequested)
                {
                    current.OnPrepareSupercede();
                }
            }
            finally
            {
                following = createFollowing();
                try
                {
                    if (current != null)
                    {
                        try { current.OnBeforeSupercededBy(following); }
                        finally { following.OnBeforeSuperceding(current); }
                    }
                }
                finally
                {
                    if (current != null && !current.IsCompleted && !current.IsCancellationRequested)
                    {
                        try
                        {
                            try
                            {
                                if (!current.IsCompleted && !current.IsCancellationRequested)
                                {
                                    current.Cancel();
                                }
                            }
                            finally { current.OnAfterSupercededBy(following); }
                        }
                        finally { following.OnAfterSuperceding(current); }
                    }
                }
            }

            return(following);
        }
예제 #5
0
 public void StartNew(TSource source, TResult value)
 {
     using (SynchronizedStateChange <SupercedableTaskState <TSource, TResult> > stateChange = ChangeState())
         stateChange.NewState = SupercedableTaskState <TSource, TResult> .CheckSupercede(stateChange.CurrentState, source, value);
 }
예제 #6
0
 public static SupercedableTaskState <TSource, TResult> ForceSupercede(SupercedableTaskState <TSource, TResult> current, TSource source, TResult value)
 {
     return(ForceSupercede(current, () => new SupercedableTaskState <TSource, TResult>(source, value)));
 }
예제 #7
0
 public static SupercedableTaskState <TSource, TResult> ForceSupercede(SupercedableTaskState <TSource, TResult> current, TSource source, Func <TSource, CancellationToken, TResult> function)
 {
     return(ForceSupercede(current, () => new SupercedableTaskState <TSource, TResult>(source, function)));
 }
예제 #8
0
 protected virtual void OnAfterSuperceding(SupercedableTaskState <TSource, TResult> previous)
 {
 }
예제 #9
0
 protected virtual void OnAfterSupercededBy(SupercedableTaskState <TSource, TResult> following)
 {
 }
예제 #10
0
 protected virtual void OnBeforeSuperceding(SupercedableTaskState <TSource, TResult> current)
 {
 }