[vb.net] How to exit an application properly

Usually, I will just use Environment.Exit(code) to exit an application. (Usually through a button click.) But I would like to know is it the proper way to exit, ie, releasing memory etc etc...

This question is related to vb.net garbage-collection

The answer is


Application.Exit
    End

will work like a charm The "END" immediately terminates further execution while "Application.Exit" closes all forms and calls.

Best regrads,


Application.Exit() does the trick too: any forms you have can still cancel this for instance if you want to present a save changes dialog.


You can use:

Me.Close

Application.Exit

End

Process immediately terminated in Task Manager Processors!

me.close()

You should try this. I guess it will work.


in this case I start Outlook and then close it

Dim ol 

Set ol = WScript.CreateObject("Outlook.Application") 'Starts Outlook

ol.quit 'Closes Outlook

In a console application, just return from the main program, in a UI-Application Close() all active Forms.

Memory from managed objects will be handled by the .NET Framework, you don't need to care about this.

If you use classes which implement IDisposable (like database connections, for example), you should call Dispose() on them when you no longer need them (preferred way: a using-Statement).

If you use such resources globally (like private members in your form), your form should implement the IDisposable pattern to release these resources on the Close()-call. See this article for details.


The following code is used in Visual Basic when prompting a user to exit the application:

Dim D As String

D = MsgBox("Are you sure you want to exit?", vbYesNo+vbQuestion,"Thanking You")

If D =  vbYes Then 
Unload Me
Else
Exit Sub
End If
End
End Sub