Link to home
Start Free TrialLog in
Avatar of JohnDoeSr
JohnDoeSr

asked on

Proper way to shut down program?

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.
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

dim frm as object

for each frm in form
 set frm = nothing
next

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

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
   
     gconnDB.Close
     Set gconnDB = Nothing    
       
End Sub
ASKER CERTIFIED SOLUTION
Avatar of DeadlyTrev
DeadlyTrev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(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..

Mike
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      
    Next


Cheers,

Leo