private int Execute(ProcessStartInfo startInfo, out string stdout, out string stderr) { logger.WriteLine("Executing: {0} {1}", startInfo.FileName, startInfo.Arguments); stopwatch.Start(); try { using (var process = Process.Start(startInfo)) { process.StandardInput.Close(); var stdoutReader = new AsyncLineReader(process.StandardOutput.BaseStream); var stderrReader = new AsyncLineReader(process.StandardError.BaseStream); var activityEvent = new ManualResetEvent(false); EventHandler activityHandler = delegate { activityEvent.Set(); }; process.Exited += activityHandler; stdoutReader.DataReceived += activityHandler; stderrReader.DataReceived += activityHandler; var stdoutBuffer = new StringBuilder(); var stderrBuffer = new StringBuilder(); while (true) { activityEvent.Reset(); while (true) { string line = stdoutReader.ReadLine(); if (line != null) { line = line.TrimEnd(); if (stdoutBuffer.Length > 0) { stdoutBuffer.AppendLine(); } stdoutBuffer.Append(line); logger.Write('>'); } else { line = stderrReader.ReadLine(); if (line != null) { line = line.TrimEnd(); if (stderrBuffer.Length > 0) { stderrBuffer.AppendLine(); } stderrBuffer.Append(line); logger.Write('!'); } else { break; } } logger.WriteLine(line); } if (process.HasExited) { break; } activityEvent.WaitOne(1000); } stdout = stdoutBuffer.ToString(); stderr = stderrBuffer.ToString(); return process.ExitCode; } } catch (FileNotFoundException e) { throw new ProcessException("Executable not found.", e, startInfo.FileName, startInfo.Arguments); } catch (Win32Exception e) { throw new ProcessException("Error executing external process.", e, startInfo.FileName, startInfo.Arguments); } finally { stopwatch.Stop(); } }
private int Execute(ProcessStartInfo startInfo, out string stdout, out string stderr) { logger.WriteLine("Executing: {0} {1}", startInfo.FileName, startInfo.Arguments); stdout = "simulated okay"; stderr = ""; return(0); stopwatch.Start(); try { using (var process = Process.Start(startInfo)) { process.StandardInput.Close(); var stdoutReader = new AsyncLineReader(process.StandardOutput.BaseStream); var stderrReader = new AsyncLineReader(process.StandardError.BaseStream); var activityEvent = new ManualResetEvent(false); EventHandler activityHandler = delegate { activityEvent.Set(); }; process.Exited += activityHandler; stdoutReader.DataReceived += activityHandler; stderrReader.DataReceived += activityHandler; var stdoutBuffer = new StringBuilder(); var stderrBuffer = new StringBuilder(); while (true) { activityEvent.Reset(); while (true) { string line = stdoutReader.ReadLine(); if (line != null) { line = line.TrimEnd(); if (stdoutBuffer.Length > 0) { stdoutBuffer.AppendLine(); } stdoutBuffer.Append(line); logger.Write('>'); } else { line = stderrReader.ReadLine(); if (line != null) { line = line.TrimEnd(); if (stderrBuffer.Length > 0) { stderrBuffer.AppendLine(); } stderrBuffer.Append(line); logger.Write('!'); } else { break; } } logger.WriteLine(line); } if (process.HasExited) { break; } activityEvent.WaitOne(1000); } stdout = stdoutBuffer.ToString(); stderr = stderrBuffer.ToString(); return(process.ExitCode); } } catch (FileNotFoundException e) { throw new ProcessException("Executable not found.", e, startInfo.FileName, startInfo.Arguments); } catch (Win32Exception e) { throw new ProcessException("Error executing external process.", e, startInfo.FileName, startInfo.Arguments); } finally { stopwatch.Stop(); } }