Solved

End Task

Posted on 2001-06-06
12
331 Views
Last Modified: 2010-05-02
When I load my application, I check to see if there is a previous instance already running.  If there is (and it's not responding), how do I End Task it (until I fix the error that caused the crash)?  I'm running NT4.
0
Comment
Question by:northeyk
[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
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6160416
try blowing it up.



lol

sorry just messing.


i have no idea
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160478
Do this to check:

Private Sub Form_Load()
If App.PrevInstance = True Then
Unload Me 'Unload if there's a previous instance if it
End If
End Sub

That's it!

glass cookie : )
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160495
> Check for an instance of your crashed program (don't know if this is applicable).

View page for code:
http://161.58.186.98/tips/tip147.html

Description: Determining Which Tasks Are Running


Terminate a program

View page for code...
http://www.freevbcode.com/ShowCode.Asp?ID=2781

Description: End a Program, Given only its Window Caption

Tha'ts it!

glass cookie : )

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160504
View page:
http://161.58.186.98/tips/tip2.html

Description: How can I close an application?

Please note that the code forgot this line (which is to be placed at the top of your code):

Public Const WM_CLOSE = &H10

That's all!

glass cookie : )
0
 
LVL 1

Expert Comment

by:dkaygee
ID: 6160510
This is an app I got from a user at this site. It is a VB app that has 1 form and 1 module:

This is the form code:

The form has 1 text box where you input the path of the app to kill and 3 cmd buttons. "Start Process", "End Process", and "Quit".

I was able to modify the code so that it worked with an existing application I was runing. I have not tested it on NT.

Option Explicit
Private Sub cmdStart_Click()
Dim sTemp As String
'
' Start the process.
'
sTemp = Trim$(txtProcess)
If sTemp = "" Then Exit Sub

glPid = Shell(sTemp, vbNormalFocus)
If glPid = 0 Then
    MsgBox "Could not start process", vbExclamation, "Error"
End If
End Sub
Private Sub cmdKill_Click()
Dim i As Long
'
' Enumerate all parent windows for the process.
'
Call fEnumWindows
'
' Send a close command to each parent window.
' The app may issue a close confirmation dialog
' depending on how it handles the WM_CLOSE message.
'
For i = 1 To colHandle.Count
    glHandle = colHandle.Item(i)
    Call SendMessage(glHandle, WM_CLOSE, 0&, 0&)
Next
End Sub
Private Sub cmdQuit_Click()
Unload Me
End Sub


This is the module code:
ption Explicit
Public glPid     As Long
Public glHandle  As Long
Public colHandle As New Collection

Public Const WM_CLOSE = &H10
Public Const WM_DESTROY = &H2

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
Dim lParent    As Long
Dim lThreadId  As Long
Dim lProcessId As Long
'
' This callback function is called by Windows (from the EnumWindows
' API call) for EVERY top-level window that exists.  It populates a
' collection with the handles of all parent windows owned by the
' process that we started.
'
fEnumWindowsCallBack = 1
lThreadId = GetWindowThreadProcessId(hwnd, lProcessId)

If glPid = lProcessId Then
    lParent = GetParent(hwnd)
    If lParent = 0 Then
        colHandle.Add hwnd
    End If
End If
End Function



Public Function fEnumWindows() As Boolean
Dim hwnd As Long
'
' The EnumWindows function enumerates all top-level windows
' by passing the handle of each window, in turn, to an
' application-defined callback function. EnumWindows
' continues until the last top-level window is enumerated or
' the callback function returns FALSE.
'
Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
End Function

0
 

Author Comment

by:northeyk
ID: 6163561
Will not work in NT4.

Also, most of the sample code that has been referenced (ie. Planet Source Code) is all written for Win9x and/or 2000.  You get DLL errors when they are run in NT4.
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6163677
both the terminateprocess and ExitProcess both work on win nt 3.1 and higher.  once you have the process id of the process they will kill it.  there is an example in the link above that shows you how to get the running processes on win 95/98/nt/2000, once you have it then terminate it.  be carefull though, the api stuff in msdn says that terminateprocess is dangerous, and can leave dll's hanging.
0
 

Author Comment

by:northeyk
ID: 6167347
jrspano.

Which download are your referring to on that page?  I assume it's "App List and Kill" (which references terminateprocess).  Running NT4, you get a Run-time Error '453' - Can't find DLL entry point RegisterServiceProcess in kernel32.

I tried four apps from the same page.  One runs (but just lists the exe's running), two of them give me the DLL error, and the other gives me Dr. Watson.
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6167371
look at the list all running processes demo.  it will show you how to the the proccess on the machine.  then use exit or terminate process.  the RegisterServiceProcess
api he is using is unknown to me.  It isn't in msdn.  if you remove the lines it will list all running exe's though.  the kill doesn't work though. just look at these demo's to learn how to get the process id.  after that you just kill it.
0
 
LVL 3

Accepted Solution

by:
jrspano earned 200 total points
ID: 6167397
once you have the process handle you call
GetExitCodeProcess to get the exit code for the process

then call ExitProcess or TerminateProcess and pass in the exit code.  you have to have sufficient rights to the terminateprocess in  win nt and 2000 to do this

for getexitcodeprocess you have to have PROCESS_QUERY_INFORMATION rights and for terminate process you have to have PROCESS_TERMINATE rights on the process.  if your process is a vb one I'm not sure what rights are on it but I would think you should have the rights you need.  If you create the process yourself in say C++ you assign the rights when you open the process
0
 

Author Comment

by:northeyk
ID: 6168863
Works great.  Thanks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

623 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