.NET End vs Form.Close() vs Application.Exit Cleaner 关闭应用程序的方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4976380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET End vs Form.Close() vs Application.Exit Cleaner way to close one's app
提问by GianT971
sometimes, when I use Form.close() when debugging my program, although the form is closed, the application is still running. I noticed this behaviour when using the msgbox function.
有时,当我在调试程序时使用 Form.close() 时,虽然表单已关闭,但应用程序仍在运行。我在使用 msgbox 函数时注意到了这种行为。
I have no thread nor timer running, so what is the best way to close a .NET app? I am using VB.NET.
我没有线程也没有计时器在运行,那么关闭 .NET 应用程序的最佳方法是什么?我正在使用 VB.NET。
Thanks
谢谢
回答by Cody Gray
The situation you describe is pretty fishy. Whenever you close your program's startup form, the entire application should quit automatically, including closing all otheropen forms. Make sure that you're closing the correct form, and you should not experience any problems.
你描述的情况很可疑。每当您关闭程序的启动窗体时,整个应用程序应自动退出,包括关闭所有其他打开的窗体。确保您关闭了正确的表单,并且不会遇到任何问题。
The other possibility is that you've changed your project (using its Properties page) not to close until allopen windows have been closed. In this mode, your application will not exit until the lastremaining open form has been closed. If you've chosen this setting, you have to make sure that you call the Closemethod of allforms that you've shown during the course of application, not just the startup/main form.
另一种可能性是您已将项目(使用其“属性”页面)更改为在所有打开的窗口都关闭之前不关闭。在这种模式下,您的应用程序在最后一个打开的表单关闭之前不会退出。如果您选择了此设置,则必须确保调用在应用程序过程中显示Close的所有表单的方法,而不仅仅是启动/主表单。
The first setting is the default for a reason, and if you've changed it, you probably want to go fix it back.
It is by far the most intuitive model for normal applications, and it prevents exactly the situation you describe. For it to work properly, make sure that you have specified your mainform as the "Startup form" (rather than a splash screen or log-in form).
由于某种原因,第一个设置是默认设置,如果您更改了它,您可能想要修复它。
到目前为止,它是最直观的正常应用模型,它可以防止出现您所描述的情况。为了使其正常工作,请确保您已将主表单指定为“启动表单”(而不是初始屏幕或登录表单)。
The settings I'm talking about are highlighted here:
我正在谈论的设置在此处突出显示:


But primarily, note that you should never have to call Application.Exitin a properly-designed application. If you find yourself having to do this in order for your program to close completely, then you are doing something wrong. Doing it is not a bad practice in itself, as long as you have a good reason. The other two answers fail to explain that, and thus I feel are incomplete at best.
但首先要Application.Exit注意,您永远不必调用设计合理的应用程序。如果您发现自己必须这样做才能完全关闭程序,那么您就做错了。这样做本身并不是一种坏习惯,只要你有充分的理由。另外两个答案未能解释这一点,因此我觉得充其量是不完整的。
回答by Josh
In .Net 1.1 and earlier, Application.Exit was not a wisechoice and the MSDN docs specifically recommended against it because all message processing stopped immediately.
在 .Net 1.1 及更早版本中,Application.Exit 不是一个明智的选择,MSDN 文档特别推荐反对它,因为所有消息处理都会立即停止。
In later versions however, calling Application.Exitwill result in Form.Close being called on all open forms in the application, thus giving you a chance to clean up after yourself, or even cancel the operation all together.
然而,在以后的版本中,调用 Application.Exit将导致在应用程序中所有打开的表单上调用Form.Close,从而让您有机会自己清理,甚至一起取消操作。
回答by Randall Spychalla
If you are in a loop (Do While, For, ...) and you call Me.Close(), you should follow with an Exit command (Exit Do, ...) or a Return()to force the message processing to terminate properly. I caught programs hanging due to this.
如果您处于循环中(Do While、For、...)并调用Me.Close(),则应遵循 Exit 命令(Exit Do, ...)或 aReturn()以强制消息处理正确终止。我发现程序因此而挂起。
回答by nullpointer
Form.Close() is use to close an instance of a Form with in .NET application it does not kill the entire application. Application.exit() kills your application.
Form.Close() 用于关闭 .NET 应用程序中的 Form 实例,它不会杀死整个应用程序。Application.exit() 会杀死您的应用程序。
回答by Jefson
Application.Exit()kills your application but there are some instances that it won't close the application.
Application.Exit()杀死您的应用程序,但在某些情况下它不会关闭应用程序。
Endis better than Application.Exit().
End比 好Application.Exit()。
回答by Jemuel Elimanco
Just put "End" keyword in your code.
只需在代码中放入“End”关键字即可。
Sub Form_Load()
Dim answer As MsgBoxResult
answer = MsgBox("Do you want to quit now?", MsgBoxStyle.YesNo)
If answer = MsgBoxResult.Yes Then
MsgBox("Terminating program")
End
End If
End Sub

