gpt4 book ai didi

.net - VB.NET 控制台应用程序中的关闭函数

转载 作者:行者123 更新时间:2023-12-02 19:43:03 24 4
gpt4 key购买 nike

使用 Environment.Exit(0) 检测到控制台关闭时,如何触发函数?

最佳答案

最简单的方法可能是处理 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 事件,这意味着上述建议不起作用),但这可能也不是您真正想要的解决方案。

关于.net - VB.NET 控制台应用程序中的关闭函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com