How could i kill a application
How would i kill an application, I'm creating my first project in VB and this is my code so far:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Shell("samp-server.exe")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
End Class
How could i kill samp-server.exe in button2?, Also how could i echo a .txt file into a TextBox?
Thanks,
Chris
Re: How could i kill a application
You shouldn't be using Shell for a start. In VB.NET you should be calling Process.Start to start a new process. You will then have a Process object to refer to. You can then call its CloseMainWindow method if it has a main window and its Kill method if it doesn't.
Re: How could i kill a application
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("samp-server.exe")
End Sub
I have completed that like you stated, But i am unsure on how i would kill this?
Re: How could i kill a application
The Process.Start method returns a Process object. You can assign that object to a variable and then access it later, e.g. in the Button2_Click method.
Re: How could i kill a application
Quote:
Originally Posted by
jmcilhinney
The Process.Start method returns a Process object. You can assign that object to a variable and then access it later, e.g. in the Button2_Click method.
This right?:
Code:
Public Class Form1
Dim samp;
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
samp = Process.Start("samp-server.exe")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Kill("samp-server.exe")
End Sub
End Class
Re: How could i kill a application
This is a very good example you should check it out.
http://www.codeproject.com/KB/vb/killVBprocess.aspx
Re: How could i kill a application
Quote:
Originally Posted by
ChrisMartino
This right?:
Code:
Public Class Form1
Dim samp;
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
samp = Process.Start("samp-server.exe")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Kill("samp-server.exe")
End Sub
End Class
If the 'samp' variable is type Porcess then you should be declaring it as type Process:Then, as I said, you are supposed to refer to that Process in the Button2_Click method. If you don't then it serves no purpose:
Re: How could i kill a application
Works BRILLIANT GUYS, So happy its my first program :D, How could i do it to check if the process is running and if it isn't it starts it up?
Re: How could i kill a application
The link that Pc_Not_Mac provided shows an example that calls Process.GetProcesses and then checks the ProcessName of each one. You could do that, or you could call Process.GetProcessesByName and specify the name of the process. If that returns an empty array then the process is not running.
Re: How could i kill a application
Quote:
Originally Posted by
jmcilhinney
The link that Pc_Not_Mac provided shows an example that calls Process.GetProcesses and then checks the ProcessName of each one. You could do that, or you could call Process.GetProcessesByName and specify the name of the process. If that returns an empty array then the process is not running.
I was thinking a timer or something? sorry im not sure on how to do a array
Re: How could i kill a application
You don't have to "do" an array. Calling GetProcessesByName will return an array. Have you checked out the link provided earlier. Also, whether you use a Timer or whatever, you're still going to have to call that method to find out whether the process is running.
If you're not sure about using arrays then maybe you should work your way through a beginner tutorial, which cover that and other basic concepts. You could also just look at a tutorial on arrays specifically.
http://www.homeandlearn.co.uk/NET/vbNET.html
http://www.startvbdotnet.com/language/arrays.aspx
Re: How could i kill a application
Ok, I found this on the internet, How would i make it to check if 'samp' is running?
Code:
If
UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
MessageBox.Show("Process running", "Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
else
MessageBox.Show("Process not running", "Message", MessageBoxButtons.OK,
MessageBoxIcon.Warning)
End
End If
Re: How could i kill a application
First up, that code is not going to work. You shouldn't use UBound at all in VB.NET anyway but that code is going to tell you that the process isn't running if UBound returns 0, which is wrong.
As I said before, you call GetProcessesByName and specify the name of the process. What's the name of the process you're looking for? That's the name you specify when you call GetProcessesByName. That will return an array of Process objects; one for each process it finds with that name. If that array is empty then there are no processes with that name. To check whether an array is empty you test its Length property.