SonarLint.VisualStudio.Progress.Controller.SequentialProgressController.Start C# (CSharp) 메소드

Start() 공개 메소드

Starts executing the initialized steps. The method is not thread safe but can be called from any thread.
public Start ( ) : System.Threading.Tasks.Task
리턴 System.Threading.Tasks.Task
        public async TPL.Task<ProgressControllerResult> Start()
        {
            if (this.IsStarted)
            {
                throw new InvalidOperationException(ProgressResources.AlreadyStartedException);
            }

            this.OnStarted();

            ProgressControllerResult controllerResult = await VsThreadingHelper.RunTask<ProgressControllerResult>(this, VsTaskRunContext.BackgroundThread,
                () =>
                {
                    ThreadHelper.ThrowIfOnUIThread();

                    // By default can abort, the individual step may changed that
                    this.CanAbort = true;

                    ProgressControllerResult result = ProgressControllerResult.Cancelled;
                    foreach (IProgressStepOperation operation in this.progressStepOperations)
                    {
                        // Try to cancel (in case the step itself will not cancel itself)
                        if (this.cancellationTokenSource.IsCancellationRequested)
                        {
                            result = ProgressControllerResult.Cancelled;
                            break;
                        }

                        this.CanAbort = operation.Step.Cancellable;

                        IProgressStepExecutionEvents notifier = this.stepFactory.GetExecutionCallback(operation);

                        // Give another try before running the operation (there's a test that covers cancellation
                        // before running the operation which requires this check after CanAbort is set)
                        if (this.cancellationTokenSource.IsCancellationRequested)
                        {
                            result = ProgressControllerResult.Cancelled;
                            break;
                        }

                        StepExecutionState stepResult = operation.Run(this.cancellationTokenSource.Token, notifier).Result;

                        /* Not trying to cancel here intentionally. The reason being
                        in case the step was the last one, there's nothing to cancel really,
                        otherwise there will be an attempt to cancel just before the next
                        step execution*/

                        if (stepResult == StepExecutionState.Succeeded)
                        {
                            result = ProgressControllerResult.Succeeded;
                        }
                        else if (stepResult == StepExecutionState.Failed)
                        {
                            result = ProgressControllerResult.Failed;
                            break;
                        }
                        else if (stepResult == StepExecutionState.Cancelled)
                        {
                            result = ProgressControllerResult.Cancelled;
                            break;
                        }
                        else
                        {
                            Debug.Fail("Unexpected step execution result:" + stepResult);
                        }
                    }

                    return result;
                },

                this.cancellationTokenSource.Token);

            this.OnFinished(controllerResult);

            return controllerResult;
        }

Usage Example

        public static IProgressEvents StartAsync(IServiceProvider sp, IProgressControlHost host, Func<IProgressController, ProgressStepDefinition[]> stepFactory)
        {
            if (sp == null)
            {
                throw new ArgumentNullException(nameof(sp));
            }

            if (host == null)
            {
                throw new ArgumentNullException(nameof(host));
            }

            if (stepFactory == null)
            {
                throw new ArgumentNullException(nameof(stepFactory));
            }

            Debug.Assert(ThreadHelper.CheckAccess(), "Expected to be called on the UI thread");

            // Initialize a controller and an observer
            var controller = new SequentialProgressController(sp);
            controller.Initialize(stepFactory(controller));

            IVsOutputWindowPane sonarLintPane = VsShellUtils.GetOrCreateSonarLintOutputPane(sp);

            bool logFullMessage;
#if DEBUG
            logFullMessage = true;
#else
            logFullMessage = false;
#endif
            var notifier = new VsOutputWindowPaneNotifier(sp,
                sonarLintPane,
                ensureOutputVisible: true,
                messageFormat: Strings.UnexpectedWorkflowError,
                logFullException: logFullMessage);
            controller.ErrorNotificationManager.AddNotifier(notifier);

            Observe(controller, host);
            controller.RunOnFinished(r => observedControllersMap.Remove(controller));
#pragma warning disable 4014 // We do want to start and forget. All the errors will be forwarded via the error notification manager
            controller.Start();
#pragma warning restore 4014

            return controller;
        }