0

Assume the following code:

let sw = new StreamWriter("out.txt", false)
sw.AutoFlush <- true

let proc = new Process()
proc.StartInfo.FileName <- path
proc.StartInfo.RedirectStandardOutput <- true
proc.StartInfo.UseShellExecute <- false
proc.OutputDataReceived.Add(fun b -> sw.WriteLine b.Data )
proc.Start() |> ignore
proc.BeginOutputReadLine()

I create a process and exit the main application. The process is still running (as it should) but it stops redirecting the standard output. Is there any way how to continue writing the standard output to the file even after the main application exits?

PS: I have to exit the main application and cannot wait for the process to finish
PPS: I would like to do a similar thing for the standard error output

Oldrich Svec
  • 4,091
  • 2
  • 25
  • 51

2 Answers2

1

I think desco's answer may work if RedirectStandardOutput is false. The output isn't being written to the file after your process exits because the OutputDataReceived event handler no longer exists. If possible, I'd recommend passing the output file path to your program (assume no path means write to stdout). With that in place it should be easy to do what you're trying to do.

Daniel
  • 46,089
  • 10
  • 89
  • 172
0

can you perform redirect by starting process with proper command line: Redirect stdout and stderr to a single file in dos?

Community
  • 1
  • 1
desco
  • 16,291
  • 1
  • 39
  • 54
  • Thanks for your answer. I have tried to do: `proc.StartInfo.Arguments -Out.txt"` but it does not seem to work. I get the output in the fsi console. – Oldrich Svec Jun 14 '11 at 12:52
  • @Oldrich: That may not be working because of `RedirectStandardOutput – Daniel Jun 14 '11 at 14:22