コード例 #1
0
        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
                       ));
        }
コード例 #2
0
        /// <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()
                   );
        }
コード例 #3
0
ファイル: Controller.cs プロジェクト: bdhero/bdhero-fat
        /// <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()
            ;
        }
コード例 #4
0
ファイル: Controller.cs プロジェクト: bdhero/bdhero-fat
 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
     );
 }
コード例 #5
0
 /// <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);
 }
コード例 #6
0
ファイル: TaskBuilder.cs プロジェクト: bdhero/bdhero-fat
 /// <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;
 }