Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #223 from github-for-unity/fixes/process-task-bugs
Browse files Browse the repository at this point in the history
Fix to process error stream async
  • Loading branch information
shana authored Aug 24, 2017
2 parents ebc6691 + afcfbc9 commit ff2344f
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions src/GitHub.Api/NewTaskSystem/ProcessTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ProcessWrapper
private readonly Action onEnd;
private readonly Action<Exception, string> onError;
private readonly CancellationToken token;
private readonly List<string> errors = new List<string>();

public Process Process { get; }
public StreamWriter Input { get; private set; }
Expand All @@ -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();
Expand All @@ -101,60 +120,48 @@ public void Run()

if (Process.StartInfo.RedirectStandardInput)
Input = new StreamWriter(Process.StandardInput.BaseStream, new UTF8Encoding(false));

var errors = new List<string>();
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()));
}
}

Expand Down

0 comments on commit ff2344f

Please sign in to comment.