public ProcessOutput Start(EventHandler onExit = null) { try { ExitHandler = onExit; ProcessStartInfo startInfo = new ProcessStartInfo(FileName, Arguments) { WorkingDirectory = WorkingDirectory, UseShellExecute = false, ErrorDialog = false, CreateNoWindow = true }; Log.AddEntry("Starting {0} working directory: {1}", FileName, WorkingDirectory); startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; ProcessOutputCollector collector = new ProcessOutputCollector((data) => FireEvent(StandardOut, new DaemonProcessEventArgs { DaemonProcess = this, Message = data }), (error) => FireEvent(ErrorOut, new DaemonProcessEventArgs { DaemonProcess = this, Message = error })); ProcessOutput = startInfo.Run(ExitHandler, collector); return(ProcessOutput); } catch (Exception ex) { FireEvent(ErrorOut, new DaemonProcessEventArgs { DaemonProcess = this, Message = ex.Message }); return(null); } }
public static ProcessOutputCollector Run(FilePath svnExecutableFilePath, string arguments, bool throwIfAnyError = true) { var outputCollector = new ProcessOutputCollector(); var runOptions = new ProcessRunOptions { Command = svnExecutableFilePath.Value, Arguments = arguments, ReceiveOutputData = outputCollector.ReceiveOutputData, ReceiveErrorData = outputCollector.ReceiveErrorData, }; ProcessRunner.Run(runOptions); if (throwIfAnyError && outputCollector.AnyError) { throw new Exception($"SVN automation failure.\nReceived:\n{outputCollector.GetErrorText()}"); } return(outputCollector); }
private static RunProcessResult RunProcessInternal(string workingDirectory, string executablePath, string parameters, TimeSpan?timeout = null, Encoding encoding = null) { timeout = timeout ?? DefaultTimeout; if (workingDirectory == null || !Directory.Exists(workingDirectory)) { throw new DeveroomConfigurationException($"Unable to find directory: {workingDirectory}"); } if (executablePath == null || !File.Exists(executablePath)) { throw new DeveroomConfigurationException($"Unable to find process: {executablePath}"); } ProcessStartInfo psi = new ProcessStartInfo(executablePath, parameters) { RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, WorkingDirectory = workingDirectory }; if (encoding != null) { psi.StandardOutputEncoding = encoding; psi.StandardErrorEncoding = encoding; } var process = new Process { StartInfo = psi, EnableRaisingEvents = true, }; var consoleOutBuilder = new StringBuilder(); var consoleErrorBuilder = new StringBuilder(); using (var outputCollector = new ProcessOutputCollector(process, consoleOutBuilder, consoleErrorBuilder)) { if (!process.Start()) { throw new InvalidOperationException("Could not start process"); } process.BeginOutputReadLine(); process.BeginErrorReadLine(); var timeOutInMilliseconds = Convert.ToInt32(timeout.Value.TotalMilliseconds); if (!process.WaitForExit(timeOutInMilliseconds) || !outputCollector.OutputWaitHandle.WaitOne(timeOutInMilliseconds) || !outputCollector.ErrorWaitHandle.WaitOne(timeOutInMilliseconds)) { throw new TimeoutException( $"Process {psi.FileName} {psi.Arguments} took longer than {timeout.Value.TotalMinutes} min to complete"); } } return(new RunProcessResult(process.ExitCode, consoleOutBuilder.ToString(), consoleErrorBuilder.ToString(), psi.FileName, psi.Arguments, psi.WorkingDirectory)); }