private static int InternalExecWaitWithCapture(string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
{
if ((cmd == null) || (cmd.Length == 0))
{
throw new ExternalException(Locale.GetText("No command provided for execution."));
}
if (outputName == null)
{
outputName = tempFiles.AddExtension("out");
}
if (errorName == null)
{
errorName = tempFiles.AddExtension("err");
}
int exit_code = -1;
Process proc = new Process();
proc.StartInfo.FileName = cmd;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.WorkingDirectory = currentDir;
try
{
proc.Start();
ProcessResultReader outReader = new ProcessResultReader(proc.StandardOutput, outputName);
ProcessResultReader errReader = new ProcessResultReader(proc.StandardError, errorName);
Thread t = new Thread(new ThreadStart(errReader.Read));
t.Start();
outReader.Read();
t.Join();
proc.WaitForExit();
}
finally
{
exit_code = proc.ExitCode;
// the handle is cleared in Close (so no ExitCode)
proc.Close();
}
return(exit_code);
}