[英]Problems getting desired output from Process.Start()
我正在研究一個應用程序,它調用幾個命令行應用程序來對某些視頻文件進行一些后期處理。
現在我正在嘗試使用Comskip從我的有線卡調諧器中識別視頻錄制中的商業廣告。 這運行得很好,但我在獲取所需的屏幕輸出時遇到問題。
String stdout = null;
using (var process = new Process())
{
var start = new ProcessStartInfo(comskip, cmdLine);
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = true;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
process.StartInfo = start;
process.Start();
process.WaitForExit();
stdout = process.StandardOutput.ReadToEnd();
}
我期待stdout
能夠抓住屏幕上顯示的內容,就像手動啟動應用程序時一樣(下面的屏幕截圖),這是應用程序正在進行的連續饋送,並且輸出中混合的是給出%的行進度,我想用它來更新進度條
但運行上面的代碼只能讓我:
使用的命令行是:“C:\\ Users \\ Chris \\ Google Drive \\ Tools \\ ComSkip \\ comskip.exe”“C:\\ Users \\ Chris \\ Desktop \\ ComSkip Tuning Files \\ Modern Family.wtv”“--ini = C: \\ Users \\ Chris \\ Desktop \\ ComSkip Tuning Files \\ comskip_ModernFamily.ini“
根據命令行將ini文件設置為C:\\ Users \\ Chris \\ Desktop \\ ComSkip Tuning Files \\ comskip_ModernFamily.ini使用C:\\ Users \\ Chris \\ Desktop \\ ComSkip Tuning Files \\ comskip_ModernFamily.ini獲取初始值。
我還嘗試重定向StandardError
流並抓取process.StandardError.ReadToEnd();
但是如果我運行這些選項,該過程似乎會掛起。
我錯過了什么來捕捉我希望的東西,或者這個應用程序的輸出流是否可能在其他無法訪問的地方?
請參閱RedirectStandardOutput
上的文檔 。 在讀取輸出之前等待子進程結束可能會導致掛起。
特別是,這個例子說不要做你做過的事情:
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
您應該使用事件OutputDataReceived
並可能使用ErrorDataReceived
並更新處理程序中的進度條。
您必須設置以下內容:
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
process.ErrorDataReceived += new DataReceivedEventHandler(ErrorOutput);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
並在ReadOutput
和ErrorOutput
捕獲輸出
private static void ErrorOutput(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
stdout = "Error: " + e.Data;
}
}
private static void ReadOutput(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
stdout = e.Data;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.