Starting a process synchronously, and "streaming" the output
- by Benjol
I'm looking at trying to start a process from F#, wait till it's finished, but also read it's output progressively.
Is this the right/best way to do it? (In my case I'm trying to execute git commands, but that is tangential to the question)
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
What I don't understand is how the proc.Start() can return a boolean and also be asynchronous enough for me to get the output out of the while progressively.
Unfortunately, I don't currently have a large enough repository - or slow enough machine, to be able to tell what order things are happening in...