Solved

Fully closing out a program

Posted on 2003-12-06
20
424 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
try something like :

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

Expert Comment

by:mccainz2
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:whose_da_n00b
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Lemme try...
0
 
LVL 3

Expert Comment

by:ashoooo
Comment Utility
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
Comment Utility
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
Comment Utility
Yeah :-D
0
 

Author Comment

by:whose_da_n00b
Comment Utility
not my fault =P

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

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

743 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

12 Experts available now in Live!

Get 1:1 Help Now