[英]Redirect and parse in realtime stdout of an long running process in vb.net
此代碼執行“handbrakecli”(命令行應用程序)並將輸出放入字符串:
Dim p As Process = New Process
p.StartInfo.FileName = "handbrakecli"
p.StartInfo.Arguments = "-i [source] -o [destination]"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start
Dim output As String = p.StandardOutput.ReadToEnd
p.WaitForExit
問題是這可能需要20分鍾才能完成,在此期間不會向用戶報告任何內容。 一旦完成,他們將看到應用程序的所有輸出,其中包括進度詳細信息。 不是很有用。
因此,我正在嘗試找到一個展示最佳方式的示例:
有沒有人有任何推薦的代碼片段?
StandardOutput屬性的類型為StreamReader,它具有ReadToEnd以外的方法。 這將是更多的代碼,但如果您使用Read方法,您可以執行其他操作,例如為用戶提供取消或報告某種類型的進度的機會。
使用代碼示例鏈接到Read方法:
http://msdn.microsoft.com/en-us/library/ath1fht8(v=VS.90).aspx
編輯:
Process類還有一個BeginOutputReadLine方法,它是一個帶回調的異步方法調用。
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline(v=VS.90).aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.