I have following situation. I need to run some bat file in c# using Process() and need to redirect it's out put end error to files. I know how to do it and my code works fine if I don't use timeout for my process. So the program works until the process ends (what I expect is the process being killed after timeout). The following is my code.
Process TcRunner = new Process();
TcRunner.StartInfo.FileName = "cmd.bat";
TcRunner.EnableRaisingEvents = true;
TcRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
TcRunner.StartInfo.UseShellExecute = false;
TcRunner.StartInfo.RedirectStandardOutput = true;
TcRunner.StartInfo.RedirectStandardError = true;
try
{
TcRunner.Start();
}
catch (Exception ex)
{
Program.Print("Error: Could not start process\n");
return 1;
}
string run_log_out = TcRunner.StandardOutput.ReadToEnd();
string run_log_err = TcRunner.StandardError.ReadToEnd();
if (!TcRunner.WaitForExit(5000)
{
//kill the process
}
try
{
StreamWriter run_out = new StreamWriter(@".\out");
StreamWriter run_err = new StreamWriter(@".\err");
run_out.WriteLine(run_log_out);
run_err.WriteLine(run_log_err);
run_out.Close();
run_err.Close();
}
catch (Exception e)
{
Console.WriteLine("Cannot open out/err for writing");
}