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); }
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); }
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); }
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); }
public void StartNew(TSource source, TResult value) { using (SynchronizedStateChange <SupercedableTaskState <TSource, TResult> > stateChange = ChangeState()) stateChange.NewState = SupercedableTaskState <TSource, TResult> .CheckSupercede(stateChange.CurrentState, source, value); }
public static SupercedableTaskState <TSource, TResult> ForceSupercede(SupercedableTaskState <TSource, TResult> current, TSource source, TResult value) { return(ForceSupercede(current, () => new SupercedableTaskState <TSource, TResult>(source, value))); }
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))); }
protected virtual void OnAfterSuperceding(SupercedableTaskState <TSource, TResult> previous) { }
protected virtual void OnAfterSupercededBy(SupercedableTaskState <TSource, TResult> following) { }
protected virtual void OnBeforeSuperceding(SupercedableTaskState <TSource, TResult> current) { }