我有以下情况。我需要使用Process()在c#中运行一些bat文件,并且需要重定向它的输出,将结束错误输出到文件中。我知道如何做到这一点,如果我不为我的进程使用超时,我的代码就能正常工作。因此,程序可以一直工作到进程结束(我期望的是超时后被终止的进程)。下面是我的代码。
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");
}发布于 2016-05-10 11:23:14
如果您想同时读取这两个流,则可能会出现死锁。因此,您应该尝试至少异步读取一个流。
在ProcessStartInfo.RedirectStandardOutput,的VisualStudio帮助中,我找到了以下信息:
// Do not perform a synchronous read to the end of both
// redirected streams.
// string output = p.StandardOutput.ReadToEnd();
// string error = p.StandardError.ReadToEnd();
// p.WaitForExit();
// Use asynchronous read operations on at least one of the streams.
p.BeginOutputReadLine();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();这个link有一个很好的例子,他们通过将方法绑定到事件来异步读取两个流:
...
build.StartInfo.RedirectStandardOutput = true;
build.StartInfo.RedirectStandardError = true;
...
build.ErrorDataReceived += build_ErrorDataReceived;
build.OutputDataReceived += build_ErrorDataReceived;
...
build.BeginOutputReadLine();
build.BeginErrorReadLine();
static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
...
} https://stackoverflow.com/questions/37131279
复制相似问题