Solved

Fully closing out a program

Posted on 2003-12-06
20
426 Views
Last Modified: 2010-05-01
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?
0
Comment
Question by:whose_da_n00b
  • 5
  • 4
  • 3
  • +4
20 Comments
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9890886
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
0
 

Author Comment

by:whose_da_n00b
ID: 9890920
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 >.<
0
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9891011
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....
0
 
LVL 24

Expert Comment

by:R_Rajesh
ID: 9891046
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
0
 
LVL 14

Accepted Solution

by:
aelatik earned 80 total points
ID: 9891698
try something like :

Private Sub Command1_Click()
  unload form1
  unload form2
  unload form3
  End
End Sub
0
 
LVL 5

Expert Comment

by:mccainz2
ID: 9892553
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
0
 
LVL 1

Expert Comment

by:Lycaon
ID: 9892757
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9894792
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
0
 
LVL 1

Expert Comment

by:Lycaon
ID: 9896449
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.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9896951
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.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:whose_da_n00b
ID: 9898965
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?
0
 
LVL 1

Expert Comment

by:Lycaon
ID: 9899073
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
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9899083
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9899240
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.
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9899498
Lemme try...
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9899527
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9899656
Right.  So whose_da_n00b must have DoEvents in his code or we wouldn't be having this thread would we?

=)

Idle_Mind
0
 
LVL 3

Expert Comment

by:ashoooo
ID: 9900060
Yeah :-D
0
 

Author Comment

by:whose_da_n00b
ID: 9920334
not my fault =P

so it will "auto" end when the loop is done?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9920724
Yes.  The app will completely close once all loops/commands have completed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

929 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

9 Experts available now in Live!

Get 1:1 Help Now