【问题标题】:RedirectStandardOutput when the main application exits using F#使用 F# 退出主应用程序时的 RedirectStandardOutput
【发布时间】:2011-09-14 16:03:12
【问题描述】:

假设以下代码:

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:我想对标准错误输出做类似的事情

【问题讨论】:

    标签: f# process redirectstandardoutput


    【解决方案1】:

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

    【讨论】:

    • 谢谢,好点子。有时我看不到最简单和最明显的解决方法:)
    【解决方案2】:

    您能否通过使用正确的命令行启动进程来执行重定向:Redirect stdout and stderr to a single file in dos

    【讨论】:

    • 感谢您的回答。我试过这样做:proc.StartInfo.Arguments &lt;- arguments + " 1&gt; -Out.txt",但它似乎不起作用。我在 fsi 控制台中得到输出。
    • @Oldrich:由于RedirectStandardOutput &lt;- true,这可能不起作用。
    猜你喜欢
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多