[英]Process exits when Startinfo.RedirectStandardOutput is set true
[英]Console application immediately exits after setting RedirectStandardOutput to true
我有一个需要根据输出实时更新控制台的进程。 但是它不起作用。 控制台仅会打开和关闭,并且流程在后台运行。 我不知道我在做什么错。 这是我的代码:
private static StringBuilder sortOutput = null;
static void Main(string[] args)
{
Process process;
process = new Process();
process.StartInfo.FileName = "C:\\ffmbc\\ffmbc.exe";
//process.StartInfo.Arguments = "-i new5830df.mxf -an ";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder("");
process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
process.Exited += new EventHandler(myProcess_Exited);
process.StartInfo.RedirectStandardInput = true;
process.Start();
process.BeginOutputReadLine();
}
private static void OutputHandler(object sender, DataReceivedEventArgs outLine)
{
string line;
line = (outLine.Data.ToString());
Console.WriteLine(line);
}
private static void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine("Proccess Finished");
}
确保调用process.WaitForExit()
进行阻止,直到进程退出。
您的Main()已退出,因此控制台关闭。 在退出Main函数之前,您需要等待被调用程序完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.