将RedirectStandardOutput设置为true后,控制台应用程序立即退出

时间:2013-07-10 22:27:09

标签: c# process

我有一个需要根据输出实时更新控制台的进程。但它不起作用。控制台只是打开和关闭,进程在后台运行。我无法弄清楚我做错了什么。这是我的代码:

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");
}

2 个答案:

答案 0 :(得分:2)

请务必致电process.WaitForExit()以阻止该流程退出。

答案 1 :(得分:0)

您的Main()已退出,因此控制台关闭。在退出Main函数之前,您需要等到被调程序完成。