From 83bb4c5e2e8704199ccba1173b42cabd6227c85f Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 10 Aug 2017 18:57:15 -0400 Subject: [PATCH 1/3] Reading stream inline while leaving input to be routed via event --- src/GitHub.Api/NewTaskSystem/ProcessTask.cs | 87 +++++++++++---------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs index f929fd96b..2ac570e58 100644 --- a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs +++ b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs @@ -78,37 +78,19 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor, public void Run() { - if (Process.StartInfo.RedirectStandardOutput) + 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); - }; + throw new ArgumentException("Process must RedirectStandardOutput"); } - if (Process.StartInfo.RedirectStandardError) + if (!Process.StartInfo.RedirectStandardError) { - Process.ErrorDataReceived += (s, e) => - { - //if (e.Data != null) - //{ - // Logger.Trace("ErrorData \"" + (e.Data == null ? "'null'" : e.Data) + "\""); - //} + throw new ArgumentException("Process must RedirectStandardError"); + } - string encodedData = null; - if (e.Data != null) - { - encodedData = Encoding.UTF8.GetString(Encoding.Default.GetBytes(e.Data)); - errors.Add(encodedData); - } - }; + if (!Process.StartInfo.CreateNoWindow) + { + throw new ArgumentException("Process must CreateNoWindow"); } try @@ -133,34 +115,55 @@ 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(); - if (Process.StartInfo.CreateNoWindow) + var outputStream = Process.StandardOutput; + var line = outputStream.ReadLine(); + while (line != null) { - while (!WaitForExit(500)) + outputProcessor.LineReceived(line); + + if (token.IsCancellationRequested) { - if (token.IsCancellationRequested) - { - if (!Process.HasExited) - Process.Kill(); - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); - } + if (!Process.HasExited) + Process.Kill(); + + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); } - if (Process.ExitCode != 0 && errors.Count > 0) + line = outputStream.ReadLine(); + } + outputProcessor.LineReceived(null); + + var errorStream = Process.StandardError; + var errorLine = errorStream.ReadLine(); + while (errorLine != null) + { + errors.Add(errorLine); + + if (token.IsCancellationRequested) { - onError?.Invoke(null, String.Join(Environment.NewLine, errors.ToArray())); + 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(); } From 5aeb27504fc3d8f5d1b2b9d065da280348170b58 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 11 Aug 2017 12:32:01 -0400 Subject: [PATCH 2/3] Fixing conditionals --- src/GitHub.Api/NewTaskSystem/ProcessTask.cs | 88 ++++++++++----------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs index 2ac570e58..1007c811d 100644 --- a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs +++ b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs @@ -78,21 +78,6 @@ public ProcessWrapper(Process process, IOutputProcessor outputProcessor, public void Run() { - if (!Process.StartInfo.RedirectStandardOutput) - { - throw new ArgumentException("Process must RedirectStandardOutput"); - } - - if (!Process.StartInfo.RedirectStandardError) - { - throw new ArgumentException("Process must RedirectStandardError"); - } - - if (!Process.StartInfo.CreateNoWindow) - { - throw new ArgumentException("Process must CreateNoWindow"); - } - try { Process.Start(); @@ -120,48 +105,57 @@ public void Run() onStart?.Invoke(); - var outputStream = Process.StandardOutput; - var line = outputStream.ReadLine(); - while (line != null) + if (Process.StartInfo.CreateNoWindow) { - outputProcessor.LineReceived(line); - - if (token.IsCancellationRequested) + if (Process.StartInfo.RedirectStandardOutput) { - if (!Process.HasExited) - Process.Kill(); + var outputStream = Process.StandardOutput; + var line = outputStream.ReadLine(); + while (line != null) + { + outputProcessor.LineReceived(line); - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); - } + if (token.IsCancellationRequested) + { + if (!Process.HasExited) + Process.Kill(); - line = outputStream.ReadLine(); - } - outputProcessor.LineReceived(null); + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); + } - var errorStream = Process.StandardError; - var errorLine = errorStream.ReadLine(); - while (errorLine != null) - { - errors.Add(errorLine); + line = outputStream.ReadLine(); + } + outputProcessor.LineReceived(null); + } - if (token.IsCancellationRequested) + if (!Process.StartInfo.RedirectStandardError) { - if (!Process.HasExited) - Process.Kill(); + var errorStream = Process.StandardError; + var errorLine = errorStream.ReadLine(); + while (errorLine != null) + { + errors.Add(errorLine); - Process.Close(); - onEnd?.Invoke(); - token.ThrowIfCancellationRequested(); - } + if (token.IsCancellationRequested) + { + if (!Process.HasExited) + Process.Kill(); - errorLine = errorStream.ReadLine(); - } + Process.Close(); + onEnd?.Invoke(); + token.ThrowIfCancellationRequested(); + } - if (Process.ExitCode != 0 && errors.Count > 0) - { - onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray())); + errorLine = errorStream.ReadLine(); + } + + if (Process.ExitCode != 0 && errors.Count > 0) + { + onError?.Invoke(null, string.Join(Environment.NewLine, errors.ToArray())); + } + } } onEnd?.Invoke(); From d12c1434772cc6e442b6b0c0c4ac3f40a4476d87 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Fri, 11 Aug 2017 12:48:22 -0400 Subject: [PATCH 3/3] Fixing error redirection --- src/GitHub.Api/NewTaskSystem/ProcessTask.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GitHub.Api/NewTaskSystem/ProcessTask.cs b/src/GitHub.Api/NewTaskSystem/ProcessTask.cs index 1007c811d..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; } @@ -103,8 +102,9 @@ public void Run() 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) { if (Process.StartInfo.RedirectStandardOutput) @@ -130,7 +130,7 @@ public void Run() outputProcessor.LineReceived(null); } - if (!Process.StartInfo.RedirectStandardError) + if (Process.StartInfo.RedirectStandardError) { var errorStream = Process.StandardError; var errorLine = errorStream.ReadLine();