0

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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    Please do not link to other sites. This can be considered shameless self-promotion and/or spam. Please make an effort to describe the problem in the question itself. Thank you. – mechanical_meat Feb 20 '13 at 20:44

1 Answers1

1

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
neutrino
  • 2,297
  • 4
  • 20
  • 28
  • 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 Process.Start("C:\batchfile.bat").Kill() End Sub End Class Is this what you mean? Because this doesn't work. – user2082130 Feb 20 '13 at 21:14
  • @user2082130 I've just edited my response, to clarify it. Hope that helps. – neutrino Feb 20 '13 at 21:42
  • i'm not sure how to set that up? whenever i try to set that script/code up properly i receive squiggles and errors. – user2082130 Feb 21 '13 at 01:54