How to terminate a running application?

Let me be specific about my question.

After I run a visual basic project, I want to terminate the running application by pressing a Button in a form.

I made the exe file from the project I wrote. After I run the exe file, and after I am done with the application I am closing the application to terminate it. After I close the application in the Windows Task Manager I still see the .exe file in the processes tab (of windows Task Manager) and the name of the running application in the Application tab.

The number of times that I access the exe file, that many number of .exe files are in the Windows task manager(In the processes tab). So I am in a need of terminating each application and .exe by pressing the end task button in the windows task manager.

Basically I need the Visual Basic code which I could put under a Button_click method in a form. So that I could press it to terminate the application without leaving the .exe still running in the windows task manager. If any one knows the solution to this please help me on  this.

Thank you.

regards,
Thas
thasAsked:
Who is Participating?
 
DocMConnect With a Mentor Commented:
Here is code to check that all the forms unload correctly.

Private Sub Form_QueryUnload(CANCEL As Integer, UnloadMode As Integer)
 Dim FRM As Form

   If (CANCEL = 0) Then
       For Each FRM In Forms
           If (FRM.Name <> Me.Name) Then Unload FRM
       Next
       For Each FRM In Forms
           If (FRM.Name <> Me.Name) Then
            CANCEL = 1 ' Do not unload Me.
            MsgBox FRM.Name & ": refused to Unload."
           End If
       Next
   End If
End Sub
0
 
vinnyd79Commented:
Unload Me
0
 
cocomobasicCommented:
' hi, try this

Private Sub Command1_Click()

    End

End Sub
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
SilentRageCommented:
Here's the dirty way to make sure that the program shuts down no matter what.  Sometimes if you have loops going on, they can reload the form you're trying to close.  You SHOULD fix those kind of bugs, but you can remember this...

Private Sub cmdExit_Click()
   End
End Sub
0
 
SilentRageCommented:
darnit.  heh, we both posted at the same time.
0
 
irfanazamCommented:
Hi,

there are number of things to consider in such situation.

1). you may be terminating your application by unloading your form using something like "Unload Me". In this case your current form will be unloaded but if there is some other form of your application is left loaded in memory then your application will not terminated and will be shown in task manager.

2). you may be terminating your application using END. this should terminate application properly without any forms to be left loaded. but some times we still get the same probelm. but chances are very few.

3). If you want to get rid of this situation make sure to unload each form explicitly like this

For Each FRM In Forms
Unload FRM
Next

4). you should always take care of if you access some property of a form or some control on a form and that form is not loaded then in above two situations that form will be loaded into memory. but will not be visible. for example you have 2 forms in your application Form1 and Form2. your Form1 is your main form and is visible but Form2 is not loaded. then if you access some property of Form2 or some control on Form2 then this will also load Form2. and about 80% programmers never take care such things. so the explicit unload as i have mentioned in point 3 saves us from this problem.

Hope you will get my point.
0
 
SilentRageCommented:
2). you may be terminating your application using END. this should terminate application properly without any forms to be left loaded. but some times we still get the same probelm. but chances are very few.

I disagree.  I've never heard of a program NOT closing immediately when execution hits the End statement.  The only bad thing about End is that it will not execute the Unload events for a form.
0
 
irfanazamCommented:
But i have encountered some situations when END made my application to crash. Once i was using a third party activex to have a custom menu like XP in VB6 application. whenever i called END in some event of that menu control my application crashed immediately then i explicilty unloaded all forms to terminate my application.
0
 
SilentRageCommented:
heh heh, yeah, you're right.  I guaranteed the application will end if you use End.  I didn't say that the program won't crash if you don't clean up some things first.  ;)  It might crash, but it WILL end.  :P
0
 
CleanupPingCommented:
thas:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
SilentRageCommented:
DocM should recieve the points

The method I had tried to post is the dirty method, and, I'm not the only one who posted it.  Shoot, I didn't even manage to post it first.  If nobody had replied, the solution DocM provided would be similar to the code I would've posted.  It isn't fool-proof, but when the asker wanted to do it from the button rather than designing his program better, it is the best of the relevent solutions.
0
 
DanRollinsCommented:
Thanks for the feedback SilentRage.  Moderator, my recommended disposition is:

    Accept DocM's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
0
All Courses

From novice to tech pro — start learning today.