Question Asker Question Asker - 3 months ago 27
Vb.net Question

How to correctly terminate an application?

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


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 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.

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.

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.

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


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

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.

Answer

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 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.

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

Here's the timer I use:

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