Created
June 15, 2013 21:04
-
-
Save kumavis/5789583 to your computer and use it in GitHub Desktop.
Process not exiting, emitting whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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