【发布时间】:2011-01-18 13:23:54
【问题描述】:
我希望在控制台应用程序退出时执行某个功能。我找到了很多解决方案,但没有一个对我有用。为什么下面的代码不显示 CancelKeyPress 等?
printfn "Starting a Test"
System.Console.ReadLine() |> ignore
System.Console.CancelKeyPress.Add (fun _ -> printfn "CancelKeyPress" )
System.AppDomain.CurrentDomain.ProcessExit.Add (fun _ -> printfn "ProcessExit" )
System.AppDomain.CurrentDomain.DomainUnload.Add (fun _ -> printfn "DomainUnload" )
我稍微修改了我的代码并添加了 try finally 语句,但没有任何运气。我运行应用程序,然后点击“ctrl + c”或点击“关闭按钮”
let write v = System.IO.File.AppendAllText("test.txt", v + "\n")
try
write "Starting a Test 2"
System.Console.ReadLine() |> ignore
System.Console.CancelKeyPress.Add (fun _ -> write "CancelKeyPress" )
System.AppDomain.CurrentDomain.ProcessExit.Add (fun _ -> write "ProcessExit" )
System.AppDomain.CurrentDomain.DomainUnload.Add (fun _ -> write "DomainUnload" )
finally
write "Try Finally"
【问题讨论】: