Example #1
0
        /// <inheritdoc />
        public IVisualStudio LaunchVisualStudio(VisualStudioVersion version, ILogger logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }

            Pair <string, VisualStudioVersion>?installDirAndVersion = GetVisualStudioInstallDirAndVersion(version);

            if (!installDirAndVersion.HasValue)
            {
                logger.Log(LogSeverity.Debug, string.Format("Could not find Visual Studio version '{0}'.", version));
                return(null);
            }

            string      devenvPath        = Path.Combine(installDirAndVersion.Value.First, "devenv.exe");
            ProcessTask devenvProcessTask = new ProcessTask(devenvPath, "", Environment.CurrentDirectory);

            logger.Log(LogSeverity.Debug, string.Format("Launching Visual Studio using path: '{0}'.", devenvProcessTask.ExecutablePath));
            devenvProcessTask.Start();

            System.Diagnostics.Process devenvProcess = devenvProcessTask.Process;
            if (devenvProcess != null)
            {
                int processId = devenvProcess.Id;

                Stopwatch stopwatch = Stopwatch.StartNew();
                for (;;)
                {
                    IVisualStudio visualStudio = GetVisualStudioFromProcess(processId, installDirAndVersion.Value.Second, true, logger);
                    if (visualStudio != null)
                    {
                        return(visualStudio);
                    }

                    if (stopwatch.ElapsedMilliseconds > VisualStudioAttachTimeoutMilliseconds)
                    {
                        logger.Log(LogSeverity.Debug, string.Format("Stopped waiting for Visual Studio to launch after {0} milliseconds.", VisualStudioAttachTimeoutMilliseconds));
                        break;
                    }

                    if (!devenvProcessTask.IsRunning)
                    {
                        break;
                    }

                    Thread.Sleep(500);
                }
            }

            if (devenvProcessTask.IsTerminated && devenvProcessTask.Result != null)
            {
                if (!devenvProcessTask.Result.HasValue)
                {
                    logger.Log(LogSeverity.Debug, "Failed to launch Visual Studio.", devenvProcessTask.Result.Exception);
                }
            }

            return(null);
        }
Example #2
0
        /// <summary>
        /// Starts a new process and begins watching it.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The output of the process will be logged and included as part of the test results.  It
        /// may also be examined using the <see cref="ProcessTask.ConsoleOutput" /> and
        /// <see cref="ProcessTask.ConsoleError" /> properties while the process executes and
        /// after it terminates.
        /// </para>
        /// <para>
        /// There is no need to call <see cref="WatchTask" /> on the returned task.
        /// </para>
        /// </remarks>
        /// <param name="executablePath">The path of the executable executable.</param>
        /// <param name="arguments">The arguments for the executable.</param>
        /// <param name="workingDirectory">The working directory.</param>
        /// <returns>The new thread task.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="executablePath"/>,
        /// <paramref name="arguments"/> or <paramref name="workingDirectory"/> is null.</exception>
        public static ProcessTask StartProcessTask(string executablePath, string arguments, string workingDirectory)
        {
            ProcessTask task = CreateProcessTask(executablePath, arguments, workingDirectory);

            task.Start();
            return(task);
        }
Example #3
0
            public void SetUp()
            {
                terminatedSuccessfully = new ManualResetEvent(false);

                processTask = Tasks.StartProcessTask(Path.Combine(Environment.SystemDirectory, "cmd.exe"),
                                                     "/C exit " + COR_E_STACKOVERFLOW, Environment.CurrentDirectory);
                processTask.Terminated += processTask_Terminated;
                processTask.Start();
                Assert.IsTrue(processTask.Join(TimeSpan.FromSeconds(10)), "Wait for exit");
            }
        public IEnumerator CanExecuteAndRestartProcess()
        {
            using (var test = StartTest())
                using (var processServer = new TestProcessServer(test.TaskManager, test.Environment,
                                                                 test.Configuration))
                {
                    processServer.ConnectSync();

                    var task = new ProcessTask <string>(test.TaskManager,
                                                        test.ProcessManager.DefaultProcessEnvironment,
                                                        TestApp, "-s 100", new StringOutputProcessor())
                    {
                        Affinity = TaskAffinity.None
                    };
                    task.Configure(processServer.ProcessManager, new ProcessOptions(MonitorOptions.KeepAlive, true));

                    var restartCount = 0;

                    var restarted = new TaskCompletionSource <ProcessRestartReason>();
                    processServer.ProcessManager.OnProcessRestart += (sender, args) => {
                        restartCount++;
                        if (restartCount == 2)
                        {
                            restarted.TrySetResult(args.Reason);
                        }
                    };

                    task.Start();

                    var timeout = Task.Delay(4000);
                    var wait    = Task.WhenAny(restarted.Task, timeout);
                    foreach (var frame in WaitForCompletion(wait))
                    {
                        yield return(frame);
                    }

                    Assert.True(wait.Result != timeout, "Restart did not happen on time");

                    task.Detach();

                    Assert.AreEqual(ProcessRestartReason.KeepAlive, restarted.Task.Result);
                }
        }
Example #5
0
        private void StartProcess(string hostConnectionArguments)
        {
            bool useElevation = HostSetup.Elevated && !DotNetRuntimeSupport.IsUsingMono;

            CreateTemporaryConfigurationFile();

            StringBuilder hostArguments = new StringBuilder();

            hostArguments.Append(hostConnectionArguments);

            if (HostSetup.DebuggerSetup == null)
            {
                hostArguments.Append(@" /timeout:").Append((int)WatchdogTimeout.TotalSeconds);
            }

            hostArguments.Append(@" /owner-process:").Append(Process.GetCurrentProcess().Id);

            if (HostSetup.ApplicationBaseDirectory != null)
            {
                hostArguments.Append(@" /application-base-directory:""").Append(
                    FileUtils.StripTrailingBackslash(HostSetup.ApplicationBaseDirectory)).Append('"');
            }

            foreach (string hintDirectory in HostSetup.HintDirectories)
            {
                hostArguments.Append(@" /hint-directory:""").Append(
                    FileUtils.StripTrailingBackslash(hintDirectory)).Append('"');
            }

            hostArguments.Append(@" /configuration-file:""").Append(temporaryConfigurationFilePath).Append('"');

            if (HostSetup.ShadowCopy)
            {
                hostArguments.Append(@" /shadow-copy");
            }

            if (HostSetup.DebuggerSetup != null)
            {
                hostArguments.Append(@" /debug");
            }

            hostArguments.Append(" /severity-prefix");

            if (useElevation)
            {
                hostArguments.Append(" /quiet");
            }

            severityPrefixParser = new SeverityPrefixParser();

            processTask             = CreateProcessTask(GetInstalledHostProcessPath(), hostArguments.ToString(), HostSetup.WorkingDirectory ?? Environment.CurrentDirectory);
            processTask.Terminated += HandleProcessExit;

            if (useElevation)
            {
                if (HostSetup.RuntimeVersion != null)
                {
                    throw new HostException("The host does not support a non-default RuntimeVersion with Elevation.");
                }

                processTask.UseShellExecute            = true;
                processTask.ConfigureProcessStartInfo += (sender, e) =>
                {
                    e.ProcessStartInfo.Verb                    = "runas";
                    e.ProcessStartInfo.ErrorDialog             = true;
                    e.ProcessStartInfo.ErrorDialogParentHandle = GetOwnerWindowHandle();
                };
            }
            else
            {
                processTask.CaptureConsoleOutput       = true;
                processTask.CaptureConsoleError        = true;
                processTask.ConsoleOutputDataReceived += LogConsoleOutput;
                processTask.ConsoleErrorDataReceived  += LogConsoleError;

                // Force CLR runtime version.
                string runtimeVersion = HostSetup.RuntimeVersion;
                if (runtimeVersion == null)
                {
                    runtimeVersion = DotNetRuntimeSupport.MostRecentInstalledDotNetRuntimeVersion;
                }

                if (!runtimeVersion.StartsWith("v"))
                {
                    runtimeVersion = "v" + runtimeVersion; // just in case, this is a common user error
                }
                // http://msdn.microsoft.com/en-us/library/w4atty68.aspx
                if (runtimeVersion == "v4.0")
                {
                    runtimeVersion = "v4.0.30319";
                }

                processTask.SetEnvironmentVariable("COMPLUS_Version", runtimeVersion);
            }

            processTask.Start();
        }