public Task <bool> CreateMetadataTask(CancellationToken cancellationToken, TaskStartedEventHandler start, ExceptionEventHandler fail, TaskSucceededEventHandler succeed, string mkvPath = null) { var token = cancellationToken; var optionalPhases = new[] { CreateGetMetadataPhase(token), CreateAutoDetectPhase(token), CreateRenamePhase(token, mkvPath) }; return(CreateStageTask( token, start, () => true, optionalPhases, fail, succeed )); }
/// <summary> /// Creates an asynchronous Task object that executes the given critical phase and optional phases in a background thread /// and invokes all other callbacks on the UI thread. /// </summary> /// <param name="cancellationToken"></param> /// <param name="beforeStart">Invoked on UI thread</param> /// <param name="criticalPhase">First phase to run. Must succeed (by returning <c>true</c> and not throwing an exception) for the optional phases to run. Invoked on the background thread.</param> /// <param name="optionalPhases">Collection of phases that can fail (by throwing an exception) without preventing subsequent phases from running. Invoked on the background thread.</param> /// <param name="fail">Called if the operation is canceled or the critical phase throws an exception. Invoked on the UI thread.</param> /// <param name="succeed">Called if the operation completes successfully without being canceled. Invoked on the UI thread.</param> /// <returns> /// Task object that returns <c>false</c> if the operation was canceled by the user or /// the critical phase threw an exception; otherwise <c>true</c>. /// </returns> private Task <bool> CreateStageTask(CancellationToken cancellationToken, TaskStartedEventHandler beforeStart, CriticalPhase criticalPhase, IEnumerable <OptionalPhase> optionalPhases, ExceptionEventHandler fail, TaskSucceededEventHandler succeed) { var canContinue = CreateCanContinueFunc(cancellationToken); return(new TaskBuilder() .OnThread(_callbackScheduler) .CancelWith(cancellationToken) .BeforeStart(beforeStart) .Fail(fail) .DoWork(delegate(IThreadInvoker invoker, CancellationToken token) { cancellationToken.Register(() => Logger.Warn("User canceled current operation")); if (criticalPhase()) { foreach (var phase in optionalPhases.TakeWhile(phase => canContinue())) { phase(); } if (canContinue()) { invoker.InvokeOnUIThreadAsync(_ => succeed()); return; } } // TODO: How should we handle exceptions here? // The rest of the code assumes exceptions are being handled by the plugin runner. invoker.InvokeOnUIThreadAsync(_ => fail(new ExceptionEventArgs())); }) .Build() ); }
/// <summary> /// Creates an asynchronous Task object that executes the given critical phase and optional phases in a background thread /// and invokes all other callbacks on the UI thread. /// </summary> /// <param name="cancellationToken"></param> /// <param name="beforeStart">Invoked on UI thread</param> /// <param name="criticalPhase">First phase to run. Must succeed (by returning <c>true</c> and not throwing an exception) for the optional phases to run. Invoked on the background thread.</param> /// <param name="optionalPhases">Collection of phases that can fail (by throwing an exception) without preventing subsequent phases from running. Invoked on the background thread.</param> /// <param name="fail">Called if the operation is canceled or the critical phase throws an exception. Invoked on the UI thread.</param> /// <param name="succeed">Called if the operation completes successfully without being canceled. Invoked on the UI thread.</param> /// <returns> /// Task object that returns <c>false</c> if the operation was canceled by the user or /// the critical phase threw an exception; otherwise <c>true</c>. /// </returns> private Task<bool> CreateStageTask(CancellationToken cancellationToken, TaskStartedEventHandler beforeStart, CriticalPhase criticalPhase, IEnumerable<OptionalPhase> optionalPhases, ExceptionEventHandler fail, TaskSucceededEventHandler succeed) { var canContinue = CreateCanContinueFunc(cancellationToken); return new TaskBuilder() .OnThread(_callbackScheduler) .CancelWith(cancellationToken) .BeforeStart(beforeStart) .Fail(fail) .DoWork(delegate(IThreadInvoker invoker, CancellationToken token) { cancellationToken.Register(() => Logger.Warn("User canceled current operation")); if (criticalPhase()) { foreach (var phase in optionalPhases.TakeWhile(phase => canContinue())) { phase(); } if (canContinue()) { invoker.InvokeOnUIThreadAsync(_ => succeed()); return; } } // TODO: How should we handle exceptions here? // The rest of the code assumes exceptions are being handled by the plugin runner. invoker.InvokeOnUIThreadAsync(_ => fail(new ExceptionEventArgs())); }) .Build() ; }
public Task<bool> CreateMetadataTask(CancellationToken cancellationToken, TaskStartedEventHandler start, ExceptionEventHandler fail, TaskSucceededEventHandler succeed, string mkvPath = null) { var token = cancellationToken; var optionalPhases = new[] { CreateGetMetadataPhase(token), CreateAutoDetectPhase(token), CreateRenamePhase(token, mkvPath) }; return CreateStageTask( token, start, () => true, optionalPhases, fail, succeed ); }
/// <summary> /// Runs the specified action in the UI thread after the main <see cref="DoWork"/> action runs /// and completes successfully (i.e., without throwing an exception). /// </summary> /// <param name="succeed">Action to run in the UI thread</param> /// <returns>Reference to this <c>TaskBuilder</c></returns> public TaskBuilder Succeed(TaskSucceededEventHandler succeed) { _succeed = succeed; return(this); }
/// <summary> /// Runs the specified action in the UI thread after the main <see cref="DoWork"/> action runs /// and completes successfully (i.e., without throwing an exception). /// </summary> /// <param name="succeed">Action to run in the UI thread</param> /// <returns>Reference to this <c>TaskBuilder</c></returns> public TaskBuilder Succeed(TaskSucceededEventHandler succeed) { _succeed = succeed; return this; }