diff --git a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs index f929fd96b..e1b9184e8 100644 --- a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs +++ b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs @@ -56,7 +56,6 @@ 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; } @@ -78,39 +77,6 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor, public void Run() { - if (Process.StartInfo.RedirectStandardOutput) - { - Process.OutputDataReceived += (s, e) => - { - //Logger.Trace("OutputData \"" + (e.Data == null ? "'null'" : e.Data) + "\""); - - string encodedData = null; - if (e.Data != null) - { - encodedData = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Data)); - } - outputProcessor.LineReceived(encodedData); - }; - } - - 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(); @@ -133,34 +99,65 @@ public void Run() return; } - if (Process.StartInfo.RedirectStandardOutput) - Process.BeginOutputReadLine(); - if (Process.StartInfo.RedirectStandardError) - Process.BeginErrorReadLine(); if (Process.StartInfo.RedirectStandardInput) Input = new StreamWriter(Process.StandardInput.BaseStream, new UTF8Encoding(false)); - onStart?.Invoke(); + var errors = new List(); + onStart?.Invoke(); if (Process.StartInfo.CreateNoWindow) { - while (!WaitForExit(500)) + if (Process.StartInfo.RedirectStandardOutput) { - if (token.IsCancellationRequested) + var outputStream = Process.StandardOutput; + var line = outputStream.ReadLine(); + while (line != null) { - if (!Process.HasExited) - Process.Kill(); - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); + outputProcessor.LineReceived(line); + + if (token.IsCancellationRequested) + { + if (!Process.HasExited) + Process.Kill(); + + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); + } + + line = outputStream.ReadLine(); } + outputProcessor.LineReceived(null); } - if (Process.ExitCode != 0 && errors.Count > 0) + if (Process.StartInfo.RedirectStandardError) { - onError?.Invoke(null, String.Join(Environment.NewLine, errors.ToArray())); + 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 (Process.ExitCode != 0 && errors.Count > 0) + { + onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray())); + } } } + onEnd?.Invoke(); }