我正在試圖從F#開始一個進程,等到它完成,但也讀取它的輸出逐步。同步啓動一個進程,並「流」輸出
這是正確/最好的方法嗎? (在我的情況,我試圖執行git的命令,但相切的問題)
let gitexecute (logger:string->unit) cmd =
let procStartInfo = new ProcessStartInfo(@"C:\Program Files\Git\bin\git.exe", cmd)
// Redirect to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput <- true
procStartInfo.UseShellExecute <- false;
// Do not create the black window.
procStartInfo.CreateNoWindow <- true;
// Create a process, assign its ProcessStartInfo and start it
let proc = new Process();
proc.StartInfo <- procStartInfo;
proc.Start() |> ignore
// Get the output into a string
while not proc.StandardOutput.EndOfStream do
proc.StandardOutput.ReadLine() |> logger
什麼我不明白的是proc.Start()如何返回一個布爾值,也對我來說足夠的不同步,以逐步獲得輸出。
不幸的是,我目前還沒有一個足夠大的存儲庫 - 或減緩足夠的機,能告訴的事情發生在什麼樣的順序?
UPDATE
我試過Brian的建議,它確實有效。
我的問題有點含糊。我的誤解是,我認爲Process.Start()返回整個過程的成功,而不僅僅是'開始',因此我看不到它是如何工作的。
我有一些成功使用異步工作流這種任務:http://stackoverflow.com/questions/2649161/need-help-regarding-async-and-fsi – Stringer 2010-06-17 20:59:21
我其實不知道什麼問題是這裏。上面的代碼是否工作?問題是什麼? – 2010-06-17 20:59:38
在我看來,你可以編寫tester.exe,寫幾條線到標準輸出和刷新,等待兩秒,寫另外兩行,並用它來測試這個代碼是否按你的意願行事,是的? – Brian 2010-06-17 21:08:25