/// <summary> /// Starts a new action using the currently set up event handlers. /// Won't start a new action if there is another action in progress. /// </summary> public Task Start(BeginCancellableAction beginCancellableAction) { if (inProgress) { throw new InvalidOperationException("More than one action may not be run at a time"); } ToggleActionState(true); var result = CancellableActionStarter.Start(beginCancellableAction, OnFinished); cancellationTokenSource = result.Item2; return(result.Item1); }
/// <summary> /// Starts a cancellable action and returns a cancellation token source for said action /// </summary> /// <param name="beginCancellableAction">An action that begins an action into which a cancellation token is injected</param> /// <param name="finishedCallback">A callback that runs when an action in question finishes</param> public static (Task, CancellationTokenSource) Start( BeginCancellableAction beginCancellableAction, Action finishedCallback) { if (beginCancellableAction == null) { throw new ArgumentNullException(nameof(beginCancellableAction)); } if (finishedCallback == null) { throw new ArgumentNullException(nameof(finishedCallback)); } var cancellationTokenSource = new CancellationTokenSource(); var task = beginCancellableAction(cancellationTokenSource.Token, finishedCallback); return(task, cancellationTokenSource); }