How to close external applications?

phoebe_ccy

Member
Joined
Oct 12, 2009
Messages
6
Programming Experience
Beginner
Hi, I have a button in Main screen, which calls A.exe when you pressed on it.

Process.Start("A.exe").

If I exit from Main, how can i make sure A.exe is closed too? Application.Exit in Main doesnt seems to work.
 
Something like
VB.NET:
Private MyProcess As Process

Private Sub btnOpenProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenProcess.Click
        MyProcess = Process.Start("A.exe")
End Sub

Private Sub btnKillProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKillProcess.Click
        If Not MyProcess Is Nothing Then
            If MyProcess.HasExited = False Then
                ' MyProcess.Kill() - use only as a last resort
                MyProcess.CloseMainWindow()
            End If
        End If
End Sub
 
To extend Hack's code a little:
VB.NET:
If MyProcess IsNot Nothing AndAlso Not MyProcess.HasExited Then
    MyProcess.CloseMainWindow()
    MyProcess.WaitForExit(10) 'Give the process up to 10 seconds to close.

    If Not MyProcess.HasExited Then
        MyProcess.Kill() 'Force the process to end.
    End If
End If
 
Back
Top