• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

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.
0
JohnDoeSr
Asked:
JohnDoeSr
1 Solution
 
JackOfPHCommented:
dim frm as object

for each frm in form
 set frm = nothing
next

0
 
JackOfPHCommented:
never use the end command in shutingdown a program the proper way is to unload all the forms then set all form to nothing. :)
0
 
DeadlyTrevCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
DeadlyTrevCommented:
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).
0
 
mah8473Commented:
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
0
 
DeadlyTrevCommented:
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.
0
 
DeadlyTrevCommented:
(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.
_____________________________________________________________________________________
0
 
mikemCommented:
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
0
 
Leo EikelmanCommented:
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
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now