C# Winforms:Application.Exit 与 Environment.Exit 与 Form.Close

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13046019/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:26:51  来源:igfitidea点击:

Winforms: Application.Exit vs Environment.Exit vs Form.Close

c#.netwindowsvb.netwinforms

提问by Parag Meshram

Following are the ways by which we can exit an application:

以下是我们可以退出应用程序的方式:

  1. Environment.Exit(0)
  2. Application.Exit()
  3. Form.Close()
  1. Environment.Exit(0)
  2. Application.Exit()
  3. Form.Close()

What is the difference between these three methods and when to use each one?

这三种方法有什么区别以及何时使用每种方法?

采纳答案by MartinStettner

The proper method would be Application.Exit(). According to the Documentation, it terminates all message loops and closes all windows thus giving your forms the possibility to execute their cleanup code (in Form.OnClose etc).

正确的方法是Application.Exit()。根据文档,它会终止所有消息循环并关闭所有窗口,从而使您的表单可以执行其清理代码(在 Form.OnClose 等中)。

Environment.Exitwould just kill the process. If some form has e.g. unsaved changes it would not have any chances to ask the user if he wants to save them. Also resources (database connections etc.) could not be released properly, files might not be flushed etc.

Environment.Exit只会杀死进程。如果某些表单有例如未保存的更改,它就没有机会询问用户是否要保存它们。此外,资源(数据库连接等)无法正确释放,文件可能无法刷新等。

Form.Closejust does what it says: it closes a form. If you have other forms opened (perhaps not now but in some future version of your application), the application will not terminate.

Form.Close只是做它所说的:它关闭一个表单。如果您打开了其他表单(可能不是现在,而是在您的应用程序的某个未来版本中),应用程序将不会终止。

Keep in mind that if you use multithreading, Application.Exit()will not terminate your threads (and thus the application will keep working in the background, even if the GUI is terminated). Therefore you must take measures to kill your threads, either in the main function (i.e. Program.Main()) or when in the OnCloseevent of your main form.

请记住,如果您使用多线程,Application.Exit()则不会终止您的线程(因此应用程序将继续在后台工作,即使 GUI 已终止)。因此,您必须采取措施杀死您的线程,无论是在主函数(即Program.Main())中还是在OnClose您的主窗体中。

回答by codeteq

they are all fine. but form.Close()won't close your application it closes the form and after that the main-method returns an int (exitcode).

他们都很好。但form.Close()不会关闭您的应用程序,它会关闭表单,然后主方法返回一个 int(退出代码)。

if you want that your application exits with exitcodes use Environmet.Exit(exitcode)or return the exitcode in the main-method

如果您希望您的应用程序以退出代码退出,请使用Environmet.Exit(exitcode)或返回主方法中的退出代码

回答by orr burgel

Dim forceExitTimer = New Threading.Timer(Sub() End, Nothing, 2500, Timeout.Infinite)
Application.Exit()

This method is perfect, allowing the software to close gently with application.exit before calling the brutal End command that will close the software by force

这个方法很完美,可以让软件先用application.exit轻轻关闭,然后再调用残酷的End命令,强制关闭软件