我正在考虑尝试从 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() 如何返回一个布尔值,并且足够异步,让我可以逐步获得输出。
不幸的是,我目前没有足够大的存储库 - 或足够慢的机器,无法判断事情发生的顺序......
更新
我尝试了布赖恩的建议,它确实有效。
我的问题有点含糊。我的误解是我认为 Process.Start() 返回了整个过程的成功,而不仅仅是“开始”,因此我看不出它是如何工作的。