RedirectStandardOutput当使用F#

问题描述:

主应用程序退出假定以下代码:RedirectStandardOutput当使用F#

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() 

我创建一个进程,并退出主应用程序。该进程仍在运行(它应该),但它会停止重定向标准输出。即使主应用程序退出后,如何继续将标准输出写入文件,还有什么办法吗?

PS:我不得不退出主应用程序,不能等待进程结束
PPS:我愿做类似的事情为标准错误输出

我认为如果RedirectStandardOutput为假,desco的答案可能会有效。由于OutputDataReceived事件处理程序不再存在,您的进程退出后,输出没有写入文件。如果可能的话,我建议将输出文件路径传递给你的程序(假设没有路径意味着写入标准输出)。有了这一点,应该很容易做你想做的事情。

+0

谢谢,好点。有时我看不到最简单明显的解决方法:) – 2011-06-15 05:12:46

你能与启动过程进行重定向适当的命令行:Redirect stdout and stderr to a single file in dos

+0

感谢您的回答。我试图做到:'proc.StartInfo.Arguments -Out.txt”'但它似乎不工作。我在fsi控制台中获得输出。 – 2011-06-14 12:52:06

+0

@Oldrich:由于'RedirectStandardOutput Daniel 2011-06-14 14:22:57