How do I close a process/program using it's window handle?
Hello, all.
My program uses another application to complete two seperate tasks. My program must open 2 instances of the application(as processes) at one time. The 1st process opens onLoad. The 2nd process can be opened with a button.
I am trying to program a close-button that closes the 2nd instance of that process only, using its windowhandle. How do I close the 2nd process only?
I am grabbing the handle of the very 1st process onLoad using this:
VB Code:
procMyProg_handle1 = procAll(0).MainWindowHandle
I have tried: - however, this does not reliably kill the 2nd process - It kills the 1st process sometimes.
Can I kill the process by its window handle?
Thanks.
-kaanuki-
vb2005 express edition
Re: How do I close a process/program using it's window handle?
Quote:
Originally Posted by kaanuki
Hello, all.
My program uses another application to complete two seperate tasks. My program must open 2 instances of the application(as processes) at one time. The 1st process opens onLoad. The 2nd process can be opened with a button.
I am trying to program a close-button that closes the 2nd instance of that process only, using its windowhandle. How do I close the 2nd process only?
I am grabbing the handle of the very 1st process onLoad using this:
VB Code:
procMyProg_handle1 = procAll(0).MainWindowHandle
I have tried:
- however, this does not reliably kill the 2nd process - It kills the 1st process sometimes.
Can I kill the process by its window handle?
Thanks.
-kaanuki-
vb2005 express edition
VB Code:
dim handle as integer
for i as integer = 0 to procall.length - 1
if procall(i).mainwindowhandle = handle then
procall(i).kill
exit for
endif
next
would that work?
remember to set handle to equal the handle you are ending.
Re: How do I close a process/program using it's window handle?
Don't call Kill on a Process that has a main window unless it refuses to close of its own accord. Calling Kill is like using the Task Manager to end a process. Would you do that without just clicking the Close button on the title bar first?
VB Code:
'Ask the process to exit by closing its main window.
If myProcess.CloseMainWindow() Then
'The call was successful so wait a reasonable period for the process to end normally.
myProcess.WaitForExit(5000)
End If
'Check whether the process has ended.
If Not myProcess.HasExited Then
'Force the process to end.
myProcess.Kill()
End If
Kill is for processes without a main window (not including services, which can be stopped using a ServiceController) or processes that refuse to respond to CloseMainWindow.
Re: How do I close a process/program using it's window handle?
Quote:
Originally Posted by jmcilhinney
Don't call Kill on a Process that has a main window unless it refuses to close of its own accord. Calling Kill is like using the Task Manager to end a process. Would you do that without just clicking the Close button on the title bar first?
VB Code:
'Ask the process to exit by closing its main window.
If myProcess.CloseMainWindow() Then
'The call was successful so wait a reasonable period for the process to end normally.
myProcess.WaitForExit(5000)
End If
'Check whether the process has ended.
If Not myProcess.HasExited Then
'Force the process to end.
myProcess.Kill()
End If
Kill is for processes without a main window (not including services, which can be stopped using a ServiceController) or processes that refuse to respond to CloseMainWindow.
so u modify the code to...
VB Code:
Dim handle as integer
For i as integer = 0 to procall.length - 1
If procall(i).mainwindowhandle = handle then
If procall(i).CloseMainWindow() Then
'The call was successful so wait a reasonable period for the process to end normally.
procall(i).WaitForExit(5000)
End If
'Check whether the process has ended.
If Not procall(i).HasExited Then
'Force the process to end.
procall(i).Kill()
End If
Exit for
Endif
Next
Re: How do I close a process/program using it's window handle?
You shouldn't be using window handles at all. You're the one creating this process in the first place so just keep a reference to it.
VB Code:
Private proc1 As Process
Private proc2 As Process
Private Sub StartProc1()
Me.proc1 = Process.Start("executable path here")
End Sub
Private Sub StartProc2()
Me.proc2 = Process.Start("executable path here")
End Sub
Private Sub EndProc1()
Me.EndProc(Me.proc1)
End Sub
Private Sub EndProc2()
Me.EndProc(Me.proc2)
End Sub
Private Sub EndProc(ByVal proc As Process)
'Ask the process to exit by closing its main window.
If Me.proc.CloseMainWindow() Then
'The call was successful so wait a reasonable period for the process to end normally.
Me.proc.WaitForExit(5000)
End If
'Check whether the process has ended.
If Not Me.proc.HasExited Then
'Force the process to end.
Me.proc.Kill()
End If
End Sub