VB.NET控制台应用程序中的关闭功能
答
这样做的最简单方法可能是处理AppDomain.ProcessExit
event,这是在应用程序的父进程退出时引发的。
例如:
Module MyApp
Sub Main()
' Attach the event handler method
AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf MyApp_ProcessExit
' Do something
' ...
Environment.Exit(0)
End Sub
Private Sub MyApp_ProcessExit(sender As Object, e As EventArgs)
Console.WriteLine("App Is Exiting...")
End Sub
End Module
但调用Environment.Exit
可能不是你原来的问题的最佳解决方案。一般来说,the only time it is necessary to use this method is when there might be other foreground threads running。在这种情况下,值得研究如何优雅地终止其他线程,而不采取严厉的措施来杀死整个过程。
Environment.Exit
,尽管名称有些令人愉悦,但却是一种非常残忍的举动。它不像在Windows任务管理器中单击“结束任务”一样糟糕(并且请注意,如果您的操作是,那么,ProcessExit
事件将不会被提升,这意味着上述建议将不起作用),但它可能不是你真正想要的解决方案。