Skip to content

Instantly share code, notes, and snippets.

@kumavis
Created June 15, 2013 21:04
Show Gist options
  • Save kumavis/5789583 to your computer and use it in GitHub Desktop.
Save kumavis/5789583 to your computer and use it in GitHub Desktop.
Process not exiting, emitting whitespace
class NugBug
{
public static void Help() {
int timeout = 5000;
using (Process process = new Process())
{
process.StartInfo.FileName = "mono";
process.StartInfo.Arguments = "--runtime=v4.0 /usr/local/bin/NuGet.exe help";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) => {
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (process.WaitForExit(timeout) &&
outputWaitHandle.WaitOne(timeout) &&
errorWaitHandle.WaitOne(timeout))
{
// Process completed. Check process.ExitCode here.
UnityEngine.Debug.Log("done");
}
else
{
// Timed out.
UnityEngine.Debug.Log("timeout");
UnityEngine.Debug.Log(output);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment