Difference between revisions of "Close Without End"

From HashVB
Jump to: navigation, search
m
m (Reverted edit of 218.180.72.11, changed back to last version by Dee)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A common misconception among people new to programming is that it's OK to use the End keyword.
+
{{VB6}}
 +
So, your application isn't shutting down properly? You may think there is an easy way around this:
  
'''Well it's not.'''
+
End
  
 +
No! 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.
  
Using End is like driving into a 6 foot btick wall.  It does no clean-up, nothing, it just ends the app.
+
"What is likely to cause my application to stay running?"
  
'''Why is this bad?'''
+
An application will shut down cleanly when and only when:
  
For one it often causes memory leaks. As well as this you cannot use it if you're subclassing.
+
All forms are unloaded.  
 +
All code has stopped running.  
 +
The first item is the most common cause, and there is a relatively easy way to find the culprit. Try running the following code in the Immediate/Debug window:
  
'''So what do you do?'''
+
For Each Form In Forms: Print Form.Name: Next
  
You loop through the forms and ''Unload'' each one in turn - like this:
+
This will print the name of all the currently loaded forms. If these forms shouldn't be loaded, then you need to track down why they are still loaded. Any code that references the user interface of a form will cause it to be loaded and left.
  
  Dim f As Form
+
If you are going to have lots of forms open and you just want to shut them all down regardless, you can use a variation of the code above:
  For Each f In Forms
+
      Unload f
+
  Next f
+
  
Simple as that.
+
Dim Form As Form
 +
For Each Form In Forms
 +
  Unload Form
 +
Next
 +
 
 +
Happy endings all round...

Latest revision as of 13:15, 8 March 2006

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

So, your application isn't shutting down properly? You may think there is an easy way around this:

End

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

"What is likely to cause my application to stay running?"

An application will shut down cleanly when and only when:

All forms are unloaded. All code has stopped running. The first item is the most common cause, and there is a relatively easy way to find the culprit. Try running the following code in the Immediate/Debug window:

For Each Form In Forms: Print Form.Name: Next

This will print the name of all the currently loaded forms. If these forms shouldn't be loaded, then you need to track down why they are still loaded. Any code that references the user interface of a form will cause it to be loaded and left.

If you are going to have lots of forms open and you just want to shut them all down regardless, you can use a variation of the code above:

Dim Form As Form
For Each Form In Forms
  Unload Form
Next

Happy endings all round...