我在C#中異步讀取一個進程的輸出時遇到了問題。 我在這個網站上發現了一些其他類似的問題,但他們並沒有真正幫助我。 這裏是我做的:如何讀取以在C#中異步結束進程輸出?
- 結交新工藝
- 設置的StartInfo -FileName,參數,CreateNoWindow(真),UseShellExecute(假),RedirectStandardOutput(真)
- 事件處理程序添加到OutputDataReceived;
- 啓動進程,BeginOutputReadLine,然後WaitForExit()。
它工作正常,但啓動過程的輸出寫入一些百分比(%
),我想,但我不能,因爲我的代碼逐行地讀取和百分比顯示不出來。
例子:
%0,%1...%100
Finished.
我的輸出:
%0
Finished.
這裏是我的程序的當前代碼:
StringBuilder sBuilder = new StringBuilder();
static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
sBuilder.AppendLine(e.Data);
}
static void CommandExecutor()
{
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = /*path of the program*/,
Arguments = /*arguments*/,
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true
}
};
process.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
你已經把一個斷點在appenline方法,看看它有多少次打?在控制檯應用程序中,百分比是否覆蓋彼此或連續? – 2012-03-02 12:17:40
覆蓋百分比。我會把一個斷點看看會發生什麼...... – 2012-03-02 12:25:14
它被擊中了八次...... – 2012-03-02 12:31:11