user2082130
user2082130

Reputation: 1

How do I end a process using Visual Basic?

Basically, I've made a program that has both "RUN BOT" and "KILL BOT" buttons. My question is what code do I use to 'Kill' the bot or in other terms close out the "batchfile.bat" that it's running when the button "RUN BOT" is clicked. Appreciate all future help!

The App with both buttons

Here is the code so far:

Public Class Form1
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Process.Start("C:\batchfile.bat")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub
End Class

Upvotes: 0

Views: 610

Answers (1)

neutrino
neutrino

Reputation: 2317

Store the Process instance that the method Process.Start() returns, in an instance variable. Then, call CloseMainWindow, or Kill, the one that better suits your need.

EDIT: This works in VS 2010

Public Class Form1
Private p As Process

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    p = Process.Start("c:\batchfile.bat")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    p.Kill()
End Sub
End Class

Upvotes: 1

Related Questions