Solved

Fully closing out a program

Posted on 2003-12-06
20
427 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

770 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