如何正确终止应用程序? [英] How to correctly terminate an application?

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

问题描述

过去,我通常将以下行添加到 KeyDown 子目录中,以结束我的应用程序:

In the past I've usually added the following line to the KeyDown sub in order to end my applications:

If e.KeyCode = Keys.Escape Then End

但是,在阅读有关 End 实际功能的文档后,事实证明,就释放资源等而言,这可能是终止应用程序的最糟糕的方法之一.

However, upon reading the documentation on what End actually does, it turns out that it probably is one of the worst ways to end the application in terms of releasing resources, etc.

因此,现在我正在尝试找到终止应用程序的推荐方法,该方法可以正确处理所有资源,等等.我在SO上找到了这两个问题(此处),但我不能断定正确的答案实际上是什么.

So now I'm trying to find the recommended way to terminate the application which will properly handle all resources, etc. I've found these two questions on SO (here and here) but I can't conclude what the right answer actually is.

第一个链接上被接受的答案说,应用对所有表单使用Close()是正确的方法,并将释放正确使用的所有资源.这在具有多种形式的应用程序中不方便,建议进一步 Exit(),甚至:

The accepted answer on the first link says that applying Close() to all forms is the correct way and will release all resources used correctly. This isn't convenient in applications with multiple forms, and further down Application.Exit() is suggested, and even:

Application.Exit()
End

即使第一行失败,也可以确保程序结束.

which will definitely make sure the program ends even if the first line fails.

另一方面,在您永远不必在设计正确的应用程序中调用 Application.Exit() ",上面的内容与上面的答案相矛盾.在更高版本的.Net中, Application.Exit()会以所有形式调用 Close().

On the other hand, the accepted answer on the second link says "you should never have to call Application.Exit() in a properly-designed application", contradicting the above and an answer further down which says Application.Exit() calls Close() on all forms in later versions of .Net.

这使我感到困惑- Application.Exit()怎么了?

This has lead me to confusion — what is wrong with Application.Exit()?

如果没有问题,那么我认为最好的搭配是:

If there is no problem with it, then am I right in thinking the best one to go with is:

Application.Exit()
End

还是太过分了?否则,什么时候 Application.Exit()不能工作(除非我编写了取消它的代码)?

or is that overdoing it? Otherwise, when does Application.Exit() fail to work (except when I write code which cancels it)?

注意:尽管此问题适用于我制作的所有程序,包括具有多种形式的程序,但最近我开始使用Sockets(具有 TcpClient/Listener 类)在计算机之间建立连接,并且不胜感激.在连接中间终止程序时与此有关的任何其他信息.在我的

Note: Although this question applies to all programs I make, including ones with multiple forms, I recently have started using Sockets (with the TcpClient/Listener classes) to make connections between computers and would appreciate any additional information relating to this when terminating the program in the middle of a connection. A comment in my recent question assures me that calling Socket.Close() isn't even necessary, but now I realise that this may not be completely true since I was using End to terminate before.

推荐答案

Application.Exit将退出消息发布到该应用程序的所有消息循环中.

Application.Exit posts an exit message to all message loops for that application.

这是关闭应用程序的一种完全可接受的方法,它将导致所有表单尝试关闭.单个表单可以覆盖此行为,例如,如果它们有未保存的工作.这将使您的应用程序随后运行,因此您需要确定是否需要处理.

It's a perfectly acceptable way of closing an application and will cause all forms to attempt to shutdown. Individual forms can override this behaviour, for instance if they have unsaved work. This will leave your application running afterward so you need to decide if that needs to be handled.

说我仅在外部参与者需要关闭我的应用程序时才使用它.否则,当应用程序的主窗体关闭时,我将退出该应用程序.

Saying that I only use it if an external actor needs to shutdown my application. Otherwise I leave the app to exit when its main form closes.

您还需要考虑所拥有的任何前台线程的状态,因为它们可以关闭所有窗体,但将线程处理留在后台.

You also need to consider the state of any foreground threads you have as these can allow all your forms to close but leave the thread processing in the background.

End是一种非常强力的技术,应该用作最后的手段.设计良好的应用程序应该能够通过关闭表单或调用application.exit来关闭.过去,我曾使用过启动计时器的方法,该计时器将在调用Application.Exit之前调用End……至少我给了它一个优雅完成的机会.

End is a very brute force technique and should be used as a last resot. A well designed application should be able to shutdown by closing the forms or by calling application.exit. I have used the approach in the past of launching a timer which will call End just before I call Application.Exit ... at least I give it a chance to complete gracefully.

注意:应用程序.退出不会被阻止.因此 Application.Exit:End 也可能是 End ,这并不理想.

Note: Application.Exit doesn't block. So Application.Exit : End might as well be End which is not ideal.

这是我使用的计时器:

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

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

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