Solved

Fully closing out a program

Posted on 2003-12-06
20
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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
 

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 86

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 86

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 86

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

724 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