public Task <bool> CreateScanTask(CancellationToken cancellationToken, string bdromPath, string mkvPath = null) { SaveRecentBDROMBeforeScan(bdromPath); var token = cancellationToken; var readBDROMPhase = new CriticalPhase(() => ReadBDROM(token, bdromPath)); var optionalPhases = new[] { CreateGetMetadataPhase(token), CreateAutoDetectPhase(token), CreateRenamePhase(token, mkvPath) }; return(CreateStageTask( token, ScanStart, readBDROMPhase, optionalPhases, ScanFail, ScanSucceed )); }
public Task <bool> CreateConvertTask(CancellationToken cancellationToken, string mkvPath = null) { if (!string.IsNullOrWhiteSpace(mkvPath)) { Job.OutputPath = mkvPath; } var token = cancellationToken; var muxPhase = new CriticalPhase(() => Mux(token)); var optionalPhases = new OptionalPhase[] { () => PostProcess(token) }; return(CreateStageTask( token, ConvertStart, muxPhase, optionalPhases, ConvertFail, ConvertSucceed )); }
/// <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> CreateConvertTask(CancellationToken cancellationToken, string mkvPath = null) { if (!string.IsNullOrWhiteSpace(mkvPath)) Job.OutputPath = mkvPath; var token = cancellationToken; var muxPhase = new CriticalPhase(() => Mux(token)); var optionalPhases = new OptionalPhase[] { () => PostProcess(token) }; return CreateStageTask( token, ConvertStart, muxPhase, optionalPhases, ConvertFail, ConvertSucceed ); }
/// <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> CreateScanTask(CancellationToken cancellationToken, string bdromPath, string mkvPath = null) { var token = cancellationToken; var readBDROMPhase = new CriticalPhase(() => ReadBDROM(token, bdromPath)); var optionalPhases = new[] { CreateGetMetadataPhase(token), CreateAutoDetectPhase(token), CreateRenamePhase(token, mkvPath) }; return CreateStageTask( token, ScanStart, readBDROMPhase, optionalPhases, ScanFail, ScanSucceed ); }
/// <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 IPromise <bool> CreateStageTask(CancellationToken cancellationToken, BeforePromiseHandler <bool> beforeStart, CriticalPhase criticalPhase, IEnumerable <OptionalPhase> optionalPhases, FailurePromiseHandler <bool> fail, SuccessPromiseHandler <bool> succeed) { return(new SimplePromise(_uiContext) .CancelWith(cancellationToken) .Before(beforeStart) .Fail(fail) .Canceled(promise => fail(promise)) // TODO: Fix warning .Work(delegate(IPromise <bool> promise) { cancellationToken.Register(() => Logger.Warn("User canceled current operation")); if (criticalPhase()) { foreach (var phase in optionalPhases.TakeWhile(phase => CanContinue(promise))) { phase(); } if (CanContinue(promise)) { _uiInvoker.InvokeAsync(() => succeed(promise)); return; } } // TODO: How should we handle exceptions here? // The rest of the code assumes exceptions are being handled by the plugin runner. _uiInvoker.InvokeAsync(() => fail(promise)); }) ); }