public void Run(CancellationToken cancellationToken) { var cancelException = false; mIsCompleted = false; mWasCancelled = false; mIsInitializing = false; mResult = Result.Success; mIsInitializing = true; Cancel(); if (cancellationToken != CancellationToken.None) { mCancel = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); } else { mCancel = new CancellationTokenSource(); } try { mCommandLine.Read(CommandLine, this); mResult = InitializeApplication(); mIsInitializing = false; if (!mResult.HasError) { mIsRunning = true; mResult = Execute(mCancel.Token); } } catch (TaskCanceledException) { cancelException = true; } catch (OperationCanceledException) { cancelException = true; } catch (AggregateException exception) { mResult = Result.CreateError(exception); foreach (var inner in exception.InnerExceptions) { if (!RaiseExceptionEvent(inner)) { throw; } } } catch (Exception exception) { mResult = Result.CreateError(exception); if (!RaiseExceptionEvent(exception)) { throw; } } finally { try { mIsRunning = false; mIsInitializing = false; mIsCompleted = true; mWasCancelled = mCancel.IsCancellationRequested || cancelException; Cleanup(mWasCancelled); } finally { if (mWasCancelled) { mResult = Result.CreateError(new OperationCanceledException()); } } } }