1

I have a simple Node.js program which execute through a button click from winform, that works fine, But i need to terminate the Node.js program and close the command prompt on another button click.How can i achieve this?

Cheers Jeev

jeev
  • 478
  • 10
  • 25
  • Have you tried anything? – Mamta D Dec 28 '12 at 06:00
  • to start a process i have used Process.Start("cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe") I am sure the close function will be something like this, but have no idea how to implement :( – jeev Dec 28 '12 at 06:02
  • This can help: http://stackoverflow.com/questions/426573/how-do-you-kill-a-process-for-a-particular-user-in-net-c – Mamta D Dec 28 '12 at 06:06
  • And also this: http://stackoverflow.com/questions/1642231/how-to-kill-a-c-sharp-process?rq=1 – Mamta D Dec 28 '12 at 06:07
  • I tried with Dim ps As Process() = Process.GetProcessesByName("cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe") For Each p As Process In ps p.Kill() Next But it's showing the error"Couldnt connect to remote machine" – jeev Dec 28 '12 at 06:21
  • Try this: http://stackoverflow.com/questions/2308249/how-to-kill-a-process-in-vb-net?rq=1 – Mamta D Dec 28 '12 at 06:38
  • Still getting the same error.Can u tel me what parameter should i pass within GetProcessesByName ? – jeev Dec 28 '12 at 06:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21801/discussion-between-mamta-dalal-and-jeev1989) – Mamta D Dec 28 '12 at 06:53

2 Answers2

2

If YOU start the process, you have an object of type process. This object supports kill which will end the process immediately.

UPDATED CODE

Public Class Form1

Dim MyProcess As Process

Private Sub btnStartPrcoess_Click(sender As Object, e As EventArgs) Handles btnStartPrcoess.Click

    If MyProcess Is Nothing Then
        MyProcess = Process.Start("cmd.exe", "arguments")
    End If

End Sub

Private Sub btnKillProcess_Click(sender As Object, e As EventArgs) Handles btnKillProcess.Click

    If MyProcess IsNot Nothing Then
        MyProcess.Kill()
        MyProcess.Close()
        MyProcess = Nothing
    End If

End Sub
End Class

If you need to end the process from a different method, you need to declare the process-variable on class level of course. And even Process.Start() has a return value of type process. So there is no need to SEARCH for the process - you already know it!

UPDATE

It's more or less nonsense, to do something like this:

MyProcess = Process.Start("cmd.exe", "/k notepad.exe")

Because it simply starts the cmd.exe which then starts notepad.exe. And of course the Process now "points" to cmd.exe and not to notepad.exe. If you want to have notepad running, it's obviously the best solution to start it DIRECTLY:

MyProcess = Process.Start("notepad.exe", "arguments for notepad, if needed")
igrimpe
  • 1,775
  • 11
  • 12
  • Actually on 1 button click the process is started and now on another button click it should be killed.I tried with for close button Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim ps As Process() = System.Diagnostics.Process.GetProcessesByName("exit.exe") For Each p As Process In ps p.Kill() Next End Sub – jeev Dec 28 '12 at 07:04
  • how do i specify the process name?? is it "cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe" or just exit.exe ?? – jeev Dec 28 '12 at 07:06
  • Again: there is no need to SEARCH the process! You already have everything you need! – igrimpe Dec 28 '12 at 07:12
  • well that worked when a command window is just opened and the close button is clicked, whereas if i execute my node prog and then try the close button it wont close. is there anything else i need to include with this? – jeev Dec 28 '12 at 07:30
  • Maybe the question is, why you start cmd.exe and not the .exe you REALLY want to run? – igrimpe Dec 28 '12 at 07:36
  • How do i execute a .exe file directly? i used MyProcess = Process.Start("cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe") – jeev Dec 28 '12 at 08:49
  • MyProcess = Process.Start("C:\Users\PROG21\Desktop\chat\exit.exe") – jeev Dec 28 '12 at 08:54
2

You have used Process.Start("cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe") to start the process. So declare a global Process varible and try;

Process exeProcess; 

on start button click:

 exeProcess = Process.Start("cmd.exe", "/k C:\Users\PROG21\Desktop\chat\exit.exe");  

On stop button click;

exeProcess.Kill();
New Developer
  • 3,245
  • 10
  • 41
  • 78