开发者

RedirectStandardOutput when the main application exits using F#

开发者 https://www.devze.com 2023-03-13 02:10 出处:网络
Assume the following code: let sw = new StreamWriter(\"out.txt\", false) sw.AutoFlush <- true let proc = new Process()

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 开发者_JS百科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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消