Link to home
Start Free TrialLog in
Avatar of whose_da_n00b
whose_da_n00b

asked on

Fully closing out a program

Hi. i have a problem.

i have a multi-form program. i currently use "unload" to close out all the forms when the exit is clicked. however, it still has the program running in the background. what is a way to avoid this? i know alt-f4 should close out the enitre program, but how do i send that keystroke when the exit option is selected?
Avatar of R_Rajesh
R_Rajesh

Hi whose_da_n00b,

you can send the keystroke like this, but you are better off trying to find out whats preventing the program form exiting normally

put this in the queryunload or exit button procedure
SendKeys "%{F4}", True


Regards,
Rajesh
Avatar of whose_da_n00b

ASKER

i take that back- alt-f4 doesnt close out the enitre program.

i have no clue wuts stopping it from totally closing. ive been debugging it for the past few days with no luck >.<
how do you know that the program is still running?? is it visible in the task list
are you creating any process from within your program or something. you could try putting a msgbox in each forms queryunload so that you will know each are indeed unloading or try using the end key word....
give this a try:

Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Sub Command1_Click()
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub
ASKER CERTIFIED SOLUTION
Avatar of aelatik
aelatik
Flag of Netherlands image

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
Another method to unload forms is to use the VB.forms collection , it will only contain those forms currently loaded

Dim objForm As Form
   
For Each objForm In VB.Forms
        Unload objForm
Next
I'd agree with mccainz2's answer, since unloading all of the forms should take care of it, however, just to be on the safe side, I'd add an End statement after the Next.
A form will not unload if there is any code still running on it.  It may no longer be visible, but it will still be running in the background.  Some kind of For..Next or Do While...Loop is most likely the culprit.

Do you have anything like that perhaps running in a timer?

Idle_Mind
Heh, that's a good point, as well.  I can't remember how many times I've gone to stop a program, and it's still stuck in the background due to a For / Next loop I've got running.  Good call.
Also, if any of your forms is loaded, but hidden, will prevent your app from closing. You should remember to unload each and every form that you have loaded.

Try going through the Forms collection and print all the loaded forms.

Also, using the End statement is not a good idea, since it terminates the app abnormally. (ie. the app doesnt terminate with a normal return code). This is because some objects may not get a chance to get unloaded properly. For example, the Class_terminate method wont be called for some objects. This may cause the 'This program has caused an illegal operation...' message to be displayed.

the main form itself has many loops, yes. the other forms are just for displays...

so how would i tell it to stop the loops on an exit command?
Dim a global boolean like ExitProgram.

Then, in all of your Do Loops, change it the conditional:

'-------------------------------------------------------------------------------
Do Until ThisLoop = "done" Or ExitProgram = True

' code here

Loop

If ExitProgram = True Then ' Do some cleanup code here, and exit sub
'-------------------------------------------------------------------------------

Or, if you've got your conditional after the Loop keyword, use

'-------------------------------------------------------------------------------
Do

' Blah

Loop Until ThisLoop = "done" Or ExitProgram = True

If ExitProgram = True Then ' Do some cleanup code here, and exit sub
'-------------------------------------------------------------------------------

Hope that comes close to what you're after.

James
Usually you wont be able to close a form while its in a loop. The execution will remain inside the loop and the program will show a 'not responding' status until it gets out of the loop. So, closing a form before it gets out of the loop is not really a concern. The real concern here is to make sure that you set all the New'ed objects to Nothing, unload all the forms after you are done with them etc.
Private exitApp As Boolean

Private Sub Form_Load()
    exitApp = False
End Sub

Private Sub Command1_Click()
    Dim a As Long
   
    For a = 2147483647 To 0 Step -1
        If exitApp Then
            Exit For
        Else
            Me.Caption = a
            DoEvents
        End If
    Next a
End Sub

Private Sub Form_Unload(Cancel As Integer)
    exitApp = True
End Sub

ashoo, copy the code to a new project and hit run.  hit the x button.  all is kosher.  now comment out the "Exit For" line and try it again.  The form will disappear but the app won't close until the for loop is done.
Lemme try...
I get it. You're using a DoEvents. Thats why you can get perform other actions while the control is still in a loop.

If your app doesnt have a DoEvents, or if you dont use multithreading, the app would not get out of the loop until its done with the loop.
Right.  So whose_da_n00b must have DoEvents in his code or we wouldn't be having this thread would we?

=)

Idle_Mind
Yeah :-D
not my fault =P

so it will "auto" end when the loop is done?
Yes.  The app will completely close once all loops/commands have completed.