We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


Proper way to shut down program?

JohnDoeSr asked
Medium Priority
Last Modified: 2010-05-01
When shutting down a program of mine by clicking the Close button in the upper right or double clicking the icon in the upper left the program looks as if it shuts down. It's not a 'proper' shutdown, the program is still running sub processes but I need it to close. In Task Manager I can see it still running. One part of the program is to send text to a website's table  in the webbrowser object. After shutting the program down, it's still trying to send text to whatever window I've got open. I have to go into the task manager / Applications tab and close it out from there. I would just insert a command button with 'end' it in but I believe that's the same thing as closing the program out the way I do now. Any help would be appreciated.
Watch Question

dim frm as object

for each frm in form
 set frm = nothing

never use the end command in shutingdown a program the proper way is to unload all the forms then set all form to nothing. :)
Try putting some code in the form's QueryUnload, Unload or Terminate events.  When the user clicks 'Close', those events are fired in that order.

In QueryUnload you have an UnloadMode parameter that you can use to tell why your form is closing (windows shutdown, killed from task manager, closing normally, user clicked close in top corner, etc).

Then you can take appropriate action ... halt your background processing, prevent the close, etc.
btw: Unloading all the forms will not necessarily end a program.  (eg, if program was started from Sub Main or has other threads of execution).

ensure all forms are unloaded, all DB.connections closed etc

I use I little routine (shortened sample below) that I call on the mainform unload event

Public Sub Finishup()
'Tidy up all objects.  May already be closed/unloaded

On Error Resume Next
    'release all collections
    Set gcolWhatever = Nothing
     Set gconnDB = Nothing    
End Sub
Of course, to just kill everything your app is doing (the equivalent of End Process from the task manager), just put End in the form's Unload event.

This IS safe and fine to do as long as your app doesn't use any resources (such as DB) that should be closed and disposed of.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
(sorry ... hit submit too soon)

... and you don't have code that you need executed as your forms are unloading.

From Microsoft's VB6 Language Reference;

Note   The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events of forms and class modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing.

I think you'll find that just using end is fine, unless there are more than one form open in your app..

Each form needs to be unloaded before firing the end command.

I use the following;

    Static unloadForm As Boolean
    Dim i As Integer
    If unloadForm Then Exit Sub
    unloadForm = True
    i = Forms.Count
    For i = Forms.Count - 1 To 0 Step -1
        Unload Forms(i)
    Next i
End If

Which I picked up somewhere on the web..

Leo EikelmanDirector, IT and Business Development

This is the proper way of shutting down

Dim frm as Form

    ' Loop thru the forms collection and
    ' unload all forms from memory
    For Each frm In Forms
          frm.Hide            ' hide the form
          Unload frm          ' deactivate the form
          Set frm = Nothing   ' remove from memory      


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.