2013-07-10 65 views
1

我有一個需要根據輸出實時更新控制檯的進程。但它不工作。控制檯只是打開和關閉,而Process在後臺運行。我無法弄清楚我做錯了什麼。這裏是我的代碼:將RedirectStandardOutput設置爲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"); 
} 

回答

2

確保呼叫process.WaitForExit()阻止,直到過程退出。

+0

當我添加process.WaitForExit()窗口保持打開狀態,但不會與輸出更新。有什麼建議麼? – user2475310

0

您的Main()已退出,因此控制檯關閉。您需要等到被調用的程序完成後再退出主功能。

相關問題