How do I write an exit handler for an F# application?

核能气质少年 提交于 2019-12-23 08:39:11

问题


Subject says it all.

I want some code to run if my application is terminated by, say, ^C.


回答1:


Use AppDomain.ProcessExit (http://msdn.microsoft.com/en-us/library/system.appdomain.processexit.aspx):

System.AppDomain.CurrentDomain.ProcessExit(fun _ -> ...)



回答2:


See code below. To handle Ctrl-C in a console app, use the Console.CancelKeyPress event.

// does not work - no exception on Ctrl-C
//System.AppDomain.CurrentDomain.UnhandledException.Add(
//    fun _ -> printfn "app is about to die")

System.Console.CancelKeyPress.Add(
    fun _ -> printfn "app is about to die")
printfn "starting..."
System.Threading.Thread.Sleep(5000)  // press Ctrl-C
printfn "ended"


来源:https://stackoverflow.com/questions/3368751/how-do-i-write-an-exit-handler-for-an-f-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!