Proper way to shut down program?

Posted on 2006-03-22
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.
Question by:JohnDoeSr
    LVL 15

    Expert Comment

    dim frm as object

    for each frm in form
     set frm = nothing

    LVL 15

    Expert Comment

    never use the end command in shutingdown a program the proper way is to unload all the forms then set all form to nothing. :)
    LVL 6

    Expert Comment

    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.
    LVL 6

    Expert Comment

    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).
    LVL 5

    Expert Comment

    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
    LVL 6

    Accepted Solution

    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.
    LVL 6

    Expert Comment

    (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.
    LVL 4

    Expert Comment

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

    LVL 8

    Expert Comment

    by:Leo Eikelman
    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      



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now