Skip to content

Instantly share code, notes, and snippets.

@kensykora
Created December 17, 2013 23:35
Show Gist options
  • Save kensykora/8014777 to your computer and use it in GitHub Desktop.
Save kensykora/8014777 to your computer and use it in GitHub Desktop.
Helper method for invoking the FibPro executable
/// <summary>
/// Invokes FibPro and returns a string of the standard output
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
private FibProOutput FibPro(string args, string interactiveInput = null)
{
var output = new StringBuilder();
var error = new StringBuilder();
var resultCode = 0;
using(var process = new Process()) {
process.StartInfo = new ProcessStartInfo()
{
Arguments = args,
FileName = "fibpro.exe",
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
using (var outputWaitHandle = new AutoResetEvent(false))
using (var errorWaitHandle = new AutoResetEvent(false))
{
process.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.Append(e.Data);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.Append(e.Data);
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
if (!string.IsNullOrWhiteSpace(interactiveInput))
{
process.StandardInput.WriteLine(interactiveInput);
}
if (process.WaitForExit(TIMEOUT_MILLISECONDS) &&
outputWaitHandle.WaitOne(TIMEOUT_MILLISECONDS) &&
errorWaitHandle.WaitOne(TIMEOUT_MILLISECONDS))
{
resultCode = process.ExitCode;
}
else
{
Assert.Fail(string.Format("FibPro timed out ({0}ms)", TIMEOUT_MILLISECONDS));
}
}
}
return new FibProOutput {
StandardOut = output.ToString(),
StandardError = error.ToString(),
ExitCode = resultCode
};
}
public class FibProOutput
{
public string StandardOut { get; set; }
public string StandardError { get; set; }
public int ExitCode { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment