Example #1
0
        private IXApplication AttemptStartApplication(IXVersion versionInfo, StartupOptions_e opts,
                                                      CancellationToken cancellationToken, TimeSpan?timeout)
        {
            int curAttempt = 1;

            CancellationTokenSource appStartCancellationTokenSrc = null;

            while (curAttempt <= MAX_ATTEMPTS)
            {
                try
                {
                    var appStartTimeout = timeout.HasValue ? timeout.Value : TimeSpan.FromMinutes(5);

                    if (cancellationToken != default)
                    {
                        appStartCancellationTokenSrc = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
                    }
                    else
                    {
                        appStartCancellationTokenSrc = new CancellationTokenSource();
                    }

                    appStartCancellationTokenSrc.CancelAfter(appStartTimeout);

                    m_UserLogger.WriteLine($"Starting host application: {versionInfo.DisplayName}");

                    var app = m_AppProvider.StartApplication(versionInfo,
                                                             opts, appStartCancellationTokenSrc.Token);

                    TryAddProcessToJob(app);

                    return(app);
                }
                catch
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        throw new JobCancelledException();
                    }

                    m_UserLogger.WriteLine($"Failed to start application from attempt {curAttempt}");

                    curAttempt++;
                }
                finally
                {
                    appStartCancellationTokenSrc?.Dispose();
                }
            }

            throw new UserMessageException("Failed to start application. Operation is aborted");
        }