public ProcessExecutionResult RunProcess(BenchEnvironment env, string cwd, string executable, string arguments, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(ConEmuExecutionHost)); } if (!IsPowerShellExecutionHostRunning) { return backupHost.RunProcess(env, cwd, executable, arguments, monitoring); } if (reloadConfigBeforeNextExecution) { ReloadConfiguration(); } var collectOutput = (monitoring & ProcessMonitoring.Output) == ProcessMonitoring.Output; var response = SendCommand("exec", cwd, executable, arguments); var exitCode = 999999; var transcriptPath = default(string); foreach (var l in response) { ParseExitCode(l, ref exitCode); ParseTranscriptPath(l, ref transcriptPath); } var output = default(string); if (collectOutput && transcriptPath != null && File.Exists(transcriptPath)) { output = File.ReadAllText(transcriptPath, Encoding.Default); File.Delete(transcriptPath); } return new ProcessExecutionResult(exitCode, output); }
static void Main(string[] args) { try { ProcessInfo[] proInfo = new ProcessInfo[2]; proInfo[0] = new ProcessInfo(); proInfo[0].Name = "notepad"; proInfo[0].Path = @"C:\Windows\notepad.exe"; proInfo[0].Delay = 3; proInfo[1] = new ProcessInfo(); proInfo[1].Name = "EXCEL"; proInfo[1].Path = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013\Excel 2013.lnk"; proInfo[1].Delay = 3; ProcessMonitoring pro = new ProcessMonitoring(proInfo); pro.Start(); } catch (Exception ex) { Console.WriteLine(ex); } }
public void StartProcess(BenchEnvironment env, string cwd, string executable, string arguments, ProcessExitCallback cb, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(ConEmuExecutionHost)); } if (!IsPowerShellExecutionHostRunning) { backupHost.StartProcess(env, cwd, executable, arguments, cb, monitoring); return; } var collectOutput = (monitoring & ProcessMonitoring.Output) == ProcessMonitoring.Output; var response = SendCommand("exec", cwd, executable, arguments); AsyncManager.StartTask(() => { var exitCode = 999999; var transcriptPath = default(string); foreach (var l in response) { ParseExitCode(l, ref exitCode); ParseTranscriptPath(l, ref transcriptPath); } var output = default(string); if (collectOutput && transcriptPath != null && File.Exists(transcriptPath)) { output = File.ReadAllText(transcriptPath, Encoding.Default); File.Delete(transcriptPath); } var result = new ProcessExecutionResult(exitCode, output); cb(result); }); }
/// <summary> /// Starts a Windows process in a synchronous fashion. /// </summary> /// <param name="env">The environment variables of Bench.</param> /// <param name="cwd">The working directory, to start the process in.</param> /// <param name="exe">The path to the executable.</param> /// <param name="arguments">The string with the command line arguments.</param> /// <param name="monitoring">A flag to control the level of monitoring.</param> /// <returns>An instance of <see cref="ProcessExecutionResult"/> with the exit code /// and optionally the output of the process.</returns> /// <seealso cref="CommandLine.FormatArgumentList(string[])"/> public ProcessExecutionResult RunProcess(BenchEnvironment env, string cwd, string exe, string arguments, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(DefaultExecutionHost)); } var p = new Process(); if (!File.Exists(exe)) { throw new FileNotFoundException("The executable could not be found.", exe); } if (!Directory.Exists(cwd)) { throw new DirectoryNotFoundException("The working directory could not be found: " + cwd); } PreparePowerShellScriptExecution(ref exe, ref arguments); var collectOutput = (monitoring & ProcessMonitoring.Output) == ProcessMonitoring.Output; StringBuilder sbStd = null; StringBuilder sbErr = null; var si = new ProcessStartInfo(exe, arguments); si.UseShellExecute = false; si.WorkingDirectory = cwd; si.CreateNoWindow = collectOutput; si.RedirectStandardOutput = collectOutput; si.RedirectStandardError = collectOutput; env.Load(si.EnvironmentVariables); p.StartInfo = si; if (collectOutput) { sbStd = new StringBuilder(); p.OutputDataReceived += (s, e) => sbStd.AppendLine(e.Data); sbErr = new StringBuilder(); p.ErrorDataReceived += (s, e) => sbErr.AppendLine(e.Data); } p.Start(); if (collectOutput) { p.BeginOutputReadLine(); p.BeginErrorReadLine(); } p.WaitForExit(); if (collectOutput) { string output; try { output = FormatOutput(sbStd.ToString()) + Environment.NewLine + FormatOutput(sbErr.ToString()); } catch (Exception e) { output = e.ToString(); } return new ProcessExecutionResult(p.ExitCode, output); } else { return new ProcessExecutionResult(p.ExitCode); } }
/// <summary> /// Starts a Windows process in an asynchronous fashion. /// </summary> /// <param name="env">The environment variables of Bench.</param> /// <param name="cwd">The working directory, to start the process in.</param> /// <param name="exe">The path to the executable.</param> /// <param name="arguments">The string with the command line arguments.</param> /// <param name="cb">The handler method to call when the execution of the process finishes.</param> /// <param name="monitoring">A flag to control the level of monitoring.</param> /// <seealso cref="CommandLine.FormatArgumentList(string[])"/> public void StartProcess(BenchEnvironment env, string cwd, string exe, string arguments, ProcessExitCallback cb, ProcessMonitoring monitoring) { if (IsDisposed) { throw new ObjectDisposedException(nameof(DefaultExecutionHost)); } AsyncManager.StartTask(() => { cb(RunProcess(env, cwd, exe, arguments, monitoring)); }); }