diff --git a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs index e1b9184e8..0850defa3 100644 --- a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs +++ b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs @@ -56,6 +56,7 @@ class ProcessWrapper private readonly Action onEnd; private readonly Action onError; private readonly CancellationToken token; + private readonly List errors = new List(); public Process Process { get; } public StreamWriter Input { get; private set; } @@ -77,6 +78,24 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor, public void Run() { + if (Process.StartInfo.RedirectStandardError) + { + Process.ErrorDataReceived += (s, e) => + { + //if (e.Data != null) + //{ + // Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\""); + //} + + string encodedData = null; + if (e.Data != null) + { + encodedData = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Data)); + errors.Add(encodedData); + } + }; + } + try { Process.Start(); @@ -101,60 +120,48 @@ public void Run() if (Process.StartInfo.RedirectStandardInput) Input = new StreamWriter(Process.StandardInput.BaseStream, new UTF8Encoding(false)); - - var errors = new List(); + if (Process.StartInfo.RedirectStandardError) + Process.BeginErrorReadLine(); onStart?.Invoke(); - if (Process.StartInfo.CreateNoWindow) + + if (Process.StartInfo.RedirectStandardOutput) { - if (Process.StartInfo.RedirectStandardOutput) + var outputStream = Process.StandardOutput; + var line = outputStream.ReadLine(); + while (line != null) { - var outputStream = Process.StandardOutput; - var line = outputStream.ReadLine(); - while (line != null) - { - outputProcessor.LineReceived(line); - - if (token.IsCancellationRequested) - { - if (!Process.HasExited) - Process.Kill(); + outputProcessor.LineReceived(line); - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); - } + if (token.IsCancellationRequested) + { + if (!Process.HasExited) + Process.Kill(); - line = outputStream.ReadLine(); + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); } - outputProcessor.LineReceived(null); + + line = outputStream.ReadLine(); } + outputProcessor.LineReceived(null); + } - if (Process.StartInfo.RedirectStandardError) + if (Process.StartInfo.CreateNoWindow) + { + while (!WaitForExit(500)) { - var errorStream = Process.StandardError; - var errorLine = errorStream.ReadLine(); - while (errorLine != null) - { - errors.Add(errorLine); - - if (token.IsCancellationRequested) - { - if (!Process.HasExited) - Process.Kill(); - - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); - } - - errorLine = errorStream.ReadLine(); - } + if (token.IsCancellationRequested) + Process.Kill(); + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); + } - if (Process.ExitCode != 0 && errors.Count > 0) - { - onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray())); - } + if (Process.ExitCode != 0 && errors.Count > 0) + { + onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray())); } }