Results 1 to 5 of 5

Thread: How do I close a process/program using it's window handle?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2004
    Posts
    12

    Red face 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:
    1. procMyProg_handle1 = procAll(0).MainWindowHandle


    I have tried:
    VB Code:
    1. procAll(0).Kill
    - 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

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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:
    1. procMyProg_handle1 = procAll(0).MainWindowHandle


    I have tried:
    VB Code:
    1. procAll(0).Kill
    - 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:
    1. dim handle as integer
    2. for i as integer = 0 to procall.length - 1
    3. if procall(i).mainwindowhandle = handle then
    4. procall(i).kill
    5. exit for
    6. endif
    7. next

    would that work?

    remember to set handle to equal the handle you are ending.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 'Ask the process to exit by closing its main window.
    2. If myProcess.CloseMainWindow() Then
    3.     'The call was successful so wait a reasonable period for the process to end normally.
    4.     myProcess.WaitForExit(5000)
    5. End If
    6.  
    7. 'Check whether the process has ended.
    8. If Not myProcess.HasExited Then
    9.     'Force the process to end.
    10.     myProcess.Kill()
    11. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    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:
    1. 'Ask the process to exit by closing its main window.
    2. If myProcess.CloseMainWindow() Then
    3.     'The call was successful so wait a reasonable period for the process to end normally.
    4.     myProcess.WaitForExit(5000)
    5. End If
    6.  
    7. 'Check whether the process has ended.
    8. If Not myProcess.HasExited Then
    9.     'Force the process to end.
    10.     myProcess.Kill()
    11. 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:
    1. Dim handle as integer
    2. For i as integer = 0 to procall.length - 1
    3. If procall(i).mainwindowhandle = handle then
    4. If procall(i).CloseMainWindow() Then
    5.     'The call was successful so wait a reasonable period for the process to end normally.
    6.     procall(i).WaitForExit(5000)
    7. End If
    8.  
    9. 'Check whether the process has ended.
    10. If Not procall(i).HasExited Then
    11.     'Force the process to end.
    12.     procall(i).Kill()
    13. End If
    14. Exit for
    15. Endif
    16. Next

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private proc1 As Process
    2. Private proc2 As Process
    3.  
    4. Private Sub StartProc1()
    5.     Me.proc1 = Process.Start("executable path here")
    6. End Sub
    7.  
    8. Private Sub StartProc2()
    9.     Me.proc2 = Process.Start("executable path here")
    10. End Sub
    11.  
    12. Private Sub EndProc1()
    13.     Me.EndProc(Me.proc1)
    14. End Sub
    15.  
    16. Private Sub EndProc2()
    17.     Me.EndProc(Me.proc2)
    18. End Sub
    19.  
    20. Private Sub EndProc(ByVal proc As Process)
    21.     'Ask the process to exit by closing its main window.
    22.     If Me.proc.CloseMainWindow() Then
    23.         'The call was successful so wait a reasonable period for the process to end normally.
    24.         Me.proc.WaitForExit(5000)
    25.     End If
    26.  
    27.     'Check whether the process has ended.
    28.     If Not Me.proc.HasExited Then
    29.         'Force the process to end.
    30.         Me.proc.Kill()
    31.     End If
    32. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width