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...
UPDATE
I tried Brian's suggestion, and it does work.
My question was a bit vague. My misunderstanding was that I assumed that Process.Start() returned the success of the process as a whole, and not just of the 'Start', and thus I couldn't see how it could work.