2011-06-14 34 views
0

主應用程序退出假定以下代碼: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:我願做類似的事情爲標準錯誤輸出

回答

1

我認爲如果RedirectStandardOutput爲假,desco的答案可能會有效。由於OutputDataReceived事件處理程序不再存在,您的進程退出後,輸出沒有寫入文件。如果可能的話,我建議將輸出文件路徑傳遞給你的程序(假設沒有路徑意味着寫入標準輸出)。有了這一點,應該很容易做你想做的事情。

+0

謝謝,好點。有時我看不到最簡單明顯的解決方法:) – 2011-06-15 05:12:46

0

你能與啓動過程進行重定向適當的命令行:Redirect stdout and stderr to a single file in dos

+0

感謝您的回答。我試圖做到:'proc.StartInfo.Arguments < - arguments +「1> -Out.txt」'但它似乎不工作。我在fsi控制檯中獲得輸出。 – 2011-06-14 12:52:06

+0

@Oldrich:由於'RedirectStandardOutput < - true',這可能不起作用。 – Daniel 2011-06-14 14:22:57