The CORRECT Universal Exit Code
No attachments for this post
This article discusses the PROPER ways to end a program.. I can't believe I'm actually posting this, but after seeing another posting on here and responses where they refer to properly ending a program as "The Advanced Way".... Well.. Here we go..
Original Author: The Chameleon
Perhaps you're new to programming and are having problems shutting down your application properly? Every day I see many people use, and recommend using, the simple command End
You should NOT use End To quote from a comment on the VB Wiki:
"End is the equivalant of stopping your car by driving it into a brick wall (stolen from a DevX newsgroup years ago :o). It does NO clean up at all, it stops the process dead. This is especially bad if you are doing any form of database or hardware access."
Adding in my own comments to this, End is one of several commands that cause Visual Basic to be labeled as being horrible and inefficient in the programming community.. Commands that result in a program running improperly, using great resources, and (in this case) leaving memory leaks..
An application will shut down once All objects have been unloaded and All code has stopped running.. Unloading your objects (usually just forms) is simple.. You just Unload frmName.. You can even put it all in the QueryUnload event of your main form if you want all other forms to unload with it..
If you're in the VB IDE and your program isn't closing, check to see if a form's the culprit first.. If you run For Each Form In Forms: Print Form.Name: Next in your Intermediate window, you'll see a listing of all of your currently loaded forms.
You can also loop through all of your forms and unload all of them by:
Dim Form As Form
For Each Form In Forms
Aside from forms, you may be loading other objects manually, User Interfaces or otherwise.. Be sure to unload all of these..
And, lastly, you may have a tight loop somewhere in your code.. You shouldn't have one without knowing it's there, so if you don't know what I'm talking about, don't worry..
In a tight loop, you should ALWAYS make a boolean that can stop it.. For example, a boolean named ProgramClosing.. Set it to true in your QueryUnload event, and also check in your loop and if FormClosing then exit the loop and stop running..
Hopefully this will help clear up some things for someone...
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.