我使用Process類調用控制檯應用程序。我正在將輸出重定向到一個日誌文件,但是當我沒有重定向輸出時,控制檯窗口中的消息比我在日誌文件中時多。任何想法爲什麼?爲什麼在從進程重定向時缺少輸出?
ProcessStartInfo psi = new ProcessStartInfo();
string filename = @"ProgrammingMaster_LocalPowertrain.exe";
psi.FileName = filename;
string arguments = String.Format("{0} {1} false", InstanceId.ToString(), ManifestFolderPath);
psi.Arguments = arguments;
LogMessage(msgType.Warning, filename + " - " + arguments);
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
FBlockProcess = new Process();
FBlockProcess.StartInfo = psi;
FBlockProcess.OutputDataReceived += new DataReceivedEventHandler(FBlockProcess_OutputDataReceived);
FBlockProcess.ErrorDataReceived += new DataReceivedEventHandler(FBlockProcess_ErrorDataReceived);
FBlockProcess.Start();
FBlockProcess.BeginOutputReadLine();
FBlockProcess.BeginErrorReadLine();
在我OutputDataReceived處理我的字符串添加到ConcurrentQueue
編輯: 我要補充一點,我想捕捉實時輸出或接近它。這個過程可能需要30多分鐘才能完成,我不想等待很長時間才能看到發生了什麼。
更新: 後的前四線輸出,該OutputDataReceived事件處理程序不會被調用,即使我知道有輸出到控制檯10或15以上線路時,我不重定向。任何想法可能導致什麼?
可能與沖洗流有關嗎? – Nick 2011-05-27 12:09:23
在關閉流之前是否調用WaitForExit()? – codymanix 2011-05-27 12:17:52
@codymanix - no。我認爲使用這些事件會使這種事情變得沒有必要。 – scott 2011-05-27 12:32:56