private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var stdOut = new List <String>(); var stdErr = new List <String>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { stdErr.Add(e.Data); var handler = ErrorDataReceived; if (handler != null) { handler(s, e); } }; CurrentProcess.OutputDataReceived += (s, e) => { stdOut.Add(e.Data); var handler = OutputDataReceived; if (handler != null) { handler(s, e); } }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; if (!CurrentProcess.WaitForExit(TimeoutMiliseconds)) { throw new TimeoutException($"The process failed to exit after {TimeoutMiliseconds / 1000.0} seconds."); } RemoveNullTerminator(stdOut); RemoveNullTerminator(stdErr); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, String.Join(System.Environment.NewLine, stdOut), String.Join(System.Environment.NewLine, stdErr))); }
private async Task <CommandResult> ExecuteAsyncInternal(string executable, string args) { var output = new List <string>(); CurrentProcess = CreateProcess(executable, args); CurrentProcess.ErrorDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); ErrorDataReceived?.Invoke(s, e); }; CurrentProcess.OutputDataReceived += (s, e) => { if (e.Data == null) { return; } output.Add($"[{_label}] {e.Data}"); Console.WriteLine($"[{_label}] {e.Data}"); OutputDataReceived?.Invoke(s, e); }; var completionTask = CurrentProcess.StartAndWaitForExitAsync(); CurrentProcess.BeginOutputReadLine(); CurrentProcess.BeginErrorReadLine(); await completionTask; CurrentProcess.WaitForExit(); RemoveNullTerminator(output); return(new CommandResult( CurrentProcess.StartInfo, CurrentProcess.ExitCode, string.Join(System.Environment.NewLine, output))); }