Rash thoughts about .NET, C#, F# and Dynamics NAV.


"Every solution will only lead to new problems."

Tuesday, 13. October 2009


Redirecting process output in F#

Filed under: F# — Steffen Forkmann at 16:03 Uhr

Today I had some trouble to redirect the default and error output from a created process. So here is my final solution:

  open System.Diagnostics

  open System.Threading

 

  // outputF: string -> unit

  // errorF: string -> unit

  let runProcess outputF errorF =

    use p = new Process()

    p.StartInfo.UseShellExecute <- false

    // …

    p.StartInfo.RedirectStandardOutput <- true

    p.StartInfo.RedirectStandardError <- true

 

    p.ErrorDataReceived.Add

      (fun d –> if  d.Data <> null then errorF d.Data)

    p.OutputDataReceived.Add

      (fun d –> if d.Data <> null then outputF d.Data)

    p.Start() |> ignore

 

    p.BeginErrorReadLine()

    p.BeginOutputReadLine()    

 

    p.WaitForExit()

 

    p.ExitCode

Tags: ,

No Comments »

No comments yet.

RSS feed for comments on this post. | TrackBack URI

Leave a comment

XHTML ( You can use these tags): <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> .