Sometimes when I end a VB program a process remain running in the background. How can I close a VB program and be sure that no threads or processes remain running? Is there any code I should have in the unload event or something?
Printable View
Sometimes when I end a VB program a process remain running in the background. How can I close a VB program and be sure that no threads or processes remain running? Is there any code I should have in the unload event or something?
A VB program ends after all of its forms have been unloaded, or when the "End" statement is executed.
"End" is a harsh way to end the application is generally not recommended.
Could it be that you hid, but did not unload a form? If so, the program may appear to have ended, but a form is still in memory.
Also, when you unload a form, setting it to Nothing will clear out any form-level variables that were declared in that form. In the Form_Unload event, I usually code:
Set Form1 = Nothing
As a rule, I try to let VB execute as many of the program's events (when closing) as possible, this ensures that 'dirty' files are saved when necessary.
As Brucey says, just stopping the program isn't a good idea.
Try this code:
Code:For Each Form In Forms
Unload Form
Next
UsageCode:Sub DestroyAll()
Dim frm As Form
For Each frm In Forms
Unload frm
Set frm = Nothing
Next frm
End Sub
Code:Private Sub Command1_Click
DestroyAll
End Sub