关闭应用程序 [英] Closing Applications

查看:113
本文介绍了关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭一个C#应用程序时,什么是最好的做法?

What is best practice when closing a C# application?

我已阅读,你可以使用:

I have read that you can use:

Environment.Exit(0); or Application.Exit();

但不同的是什么呢?

But what is the difference?

此外,关于Environment.Exit(0),我已经使用退出codeS使用Java,但从来没有完全理解他们的目的时之前。什么作用退出在C#应用程序时,他们玩?

Furthermore, with regards to Environment.Exit(0), I have used exit codes before when working with Java but have never fully understood their purpose. What role do they play when exiting an application in C#?

推荐答案

System.Windows.Forms.Application.Exit() - 通知所有消息泵必须终止,然后关闭所有应用程序该消息后的窗户已被处理。这种方法停在所有线程所有正在运行的消息循环,并关闭应用程序的所有窗口。此方法不会强制应用程序退出。退出方法通常是从消息循环中调用,并强制运行返回。要只退出当前线程的消息循环,调用ExitThread。这是,如果你正在运行一个WinForms应用程序使用的电话。作为一般原则,用这个电话如果你调用System.Windows.Forms.Application.Run。

System.Windows.Forms.Application.Exit() - Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This method stops all running message loops on all threads and closes all windows of the application. This method does not force the application to exit. The Exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call ExitThread. This is the call to use if you are running a WinForms application. As a general guideline, use this call if you have called System.Windows.Forms.Application.Run.

System.Environment.Exit(退出code) - 终止这一过程,并给出了底层操作系统指定的退出code。此调用要求您已经SecurityPermissionFlag.Unmanaged code权限。如果你不这样做,会出现一个S​​ecurityException错误。这是,如果你正在运行一个控制台应用程序使用的电话。

System.Environment.Exit(exitCode) - Terminates this process and gives the underlying operating system the specified exit code. This call requires that you have SecurityPermissionFlag.UnmanagedCode permissions. If you do not, a SecurityException error occurs. This is the call to use if you are running a console application.

我希望这是最好使用 Application.Exit

检查该链接<一个href=\"http://stackoverflow.com/questions/1312885/application-exit-vs-application-exitthread-vs-environment-exit\">Application.Exit() VS Application.ExitThread()VS Environment.Exit()和<一个href=\"http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx\">http://geekswithblogs.net/mtreadwell/archive/2004/06/06/6123.aspx

这篇关于关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆