1

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

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

2 回答 2

2

确保调用process.WaitForExit()阻塞直到进程退出。

于 2013-07-10T22:31:23.730 回答
0

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

于 2013-07-10T22:32:12.407 回答