9 Replies - 14412 Views - Last Post: 15 July 2009 - 09:58 AM Rate Topic: -----

#1 Ewinz87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Kill older process with same name

Posted 27 May 2009 - 07:03 PM

I'm using the following code to kill a program:

 Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("myprogram.exe")
				For Each p As Process In pProcess
						p.Kill()
				Next



At one point it's running twice and the above code kills em all including the one currently running, resulting in an access error. Is there any way to say, kill the older myprogram.exe only. Compare both of them and see which one was executed first and terminate this one, keeping the later one running. Any help would be appreciated. :D

Is This A Good Question/Topic? 0
  • +

Replies To: Kill older process with same name

#2 mistyfyed   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-March 09

Re: Kill older process with same name

Posted 27 May 2009 - 08:37 PM

Have it catch the processId in some way?

 Dim pProcess As Process = System.Diagnostics.Process.GetProcessById(processid)
pProcess.kill()


This post has been edited by mistyfyed: 27 May 2009 - 08:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ewinz87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: Kill older process with same name

Posted 27 May 2009 - 10:56 PM

Hmm, still trying to find a way to get it to work but without success. :(
I've been reading, and this is what I came up with:

				Dim currentProcessId As Integer = System.Diagnostics.Process.GetCurrentProcess.Id
				Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("myprogram.exe")
				For Each p As Process In pProcess
					If p.Id <> currentProcessId Then
						p.Kill()
					End If
				Next



For some reasons it's not working and I can figure why. It seems it's just ignoring all of this.
Reason I'm saying is I'm not getting any error when debugging and I've added message boxes and Try/Catch at the if statement and didn't get any message.

This post has been edited by Ewinz87: 27 May 2009 - 10:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 210
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Kill older process with same name

Posted 27 May 2009 - 11:25 PM

try this:
		Try
			Dim str1(Process.GetProcesses().Length) As String
			Dim i As Integer = 0
			For Each pro As Process In Process.GetProcesses
				str1(i) = pro.ProcessName
				i += 1
			Next

			For Each pr As Process In Process.GetProcesses
				For a As Integer = 0 To str1.Length
					If (pr.ProcessName = str1(a)) Then
						pr.Kill()
					End If
				Next
			Next
		Catch ex As Exception
		End Try

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Kill older process with same name

Posted 27 May 2009 - 11:32 PM

Here's what I did to test what I'm suggesting. I created a simple form with a ListView (lstProcesses) a Button (Button1) and a Label (Label1, this displays the number of running processes). In the form's load event I cann UpdateProcessList, which fills the ListBox with the process name and it's ID

Private Sub UpdateProcessList()
	' clear the existing list of any items
	lstProcesses.Items.Clear()
	
	' loop through the running processes and add
	'each to the list
	For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
		lstProcesses.Items.Add(p.ProcessName & " - " & p.Id)
	Next
	
	' display the number of running processes in
	' a status message at the bottom of the page
	Label1.Text = "Processes running: " + lstProcesses.Items.Count.ToString()
End Sub



Then in the button I kill the selected process (by the ID, that way a program, such as Visual Studio, can be opened multiple times and I only close the one I want based on the ID. It then repopulates the ListBox with the current running processes (after killing the one I selected and killed:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	' loop through the running processes looking for a match
	' by comparing process name to the name selected in the listbox
	For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
		Dim args As String() = lstProcesses.SelectedItem.ToString().Split("-"c)
		Dim proc As String = args(0).Trim()
		Dim processID As Integer = Convert.ToInt32(args(1).Trim())
		If p.ProcessName = proc AndAlso p.Id = processID Then
			p.Kill()
		End If
	Next

	' update the list to show the killed process
	' has been removed
	UpdateProcessList()
End Sub



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#6 Ewinz87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: Kill older process with same name

Posted 28 May 2009 - 12:17 AM

View Postnoorahmad, on 27 May, 2009 - 10:25 PM, said:

try this:
		Try
			Dim str1(Process.GetProcesses().Length) As String
			Dim i As Integer = 0
			For Each pro As Process In Process.GetProcesses
				str1(i) = pro.ProcessName
				i += 1
			Next

			For Each pr As Process In Process.GetProcesses
				For a As Integer = 0 To str1.Length
					If (pr.ProcessName = str1(a)) Then
						pr.Kill()
					End If
				Next
			Next
		Catch ex As Exception
		End Try


I don't understand the code.

View PostPsychoCoder, on 27 May, 2009 - 10:32 PM, said:

Here's what I did to test what I'm suggesting. I created a simple form with a ListView (lstProcesses) a Button (Button1) and a Label (Label1, this displays the number of running processes). In the form's load event I cann UpdateProcessList, which fills the ListBox with the process name and it's ID

Private Sub UpdateProcessList()
	' clear the existing list of any items
	lstProcesses.Items.Clear()
	
	' loop through the running processes and add
	'each to the list
	For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
		lstProcesses.Items.Add(p.ProcessName & " - " & p.Id)
	Next
	
	' display the number of running processes in
	' a status message at the bottom of the page
	Label1.Text = "Processes running: " + lstProcesses.Items.Count.ToString()
End Sub



Then in the button I kill the selected process (by the ID, that way a program, such as Visual Studio, can be opened multiple times and I only close the one I want based on the ID. It then repopulates the ListBox with the current running processes (after killing the one I selected and killed:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	' loop through the running processes looking for a match
	' by comparing process name to the name selected in the listbox
	For Each p As System.Diagnostics.Process In System.Diagnostics.Process.GetProcesses()
		Dim args As String() = lstProcesses.SelectedItem.ToString().Split("-"c)
		Dim proc As String = args(0).Trim()
		Dim processID As Integer = Convert.ToInt32(args(1).Trim())
		If p.ProcessName = proc AndAlso p.Id = processID Then
			p.Kill()
		End If
	Next

	' update the list to show the killed process
	' has been removed
	UpdateProcessList()
End Sub



Hope that helps :)


I'd like to have it fully automated without having myself interact with the window form.
Was This Post Helpful? 0
  • +
  • -

#7 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 210
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Kill older process with same name

Posted 28 May 2009 - 12:48 AM

i my example i used nested loop.
for automatic use a timer

This post has been edited by noorahmad: 28 May 2009 - 12:49 AM

Was This Post Helpful? 0
  • +
  • -

#8 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 66
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: Kill older process with same name

Posted 28 May 2009 - 07:39 AM

Your code works except that process names don't end with .exe and it wont work when in debug mode because the process name will be myprogram.vshost.

I put the your code (with corrected processname) into a console app and it works np.
  Dim currentProcessId As Integer = System.Diagnostics.Process.GetCurrentProcess.Id
		   Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("KillTest")
		   For Each p As Process In pProcess
			   If p.Id <> currentProcessId Then
				   p.Kill()
			   End If
		   Next


Was This Post Helpful? 1
  • +
  • -

#9 Ewinz87   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-May 09

Re: Kill older process with same name

Posted 28 May 2009 - 07:58 PM

View PostT3hC13h, on 28 May, 2009 - 06:39 AM, said:

Your code works except that process names don't end with .exe and it wont work when in debug mode because the process name will be myprogram.vshost.

I put the your code (with corrected processname) into a console app and it works np.
  Dim currentProcessId As Integer = System.Diagnostics.Process.GetCurrentProcess.Id
		   Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("KillTest")
		   For Each p As Process In pProcess
			   If p.Id <> currentProcessId Then
				   p.Kill()
			   End If
		   Next



Oh, I didn't know it didn't need the .exe! Just tested without it and works just fine. Such a little mistake, thanks alot. Thanks alot to the other who replied with solution aswell!
Was This Post Helpful? 0
  • +
  • -

#10 nmgod   User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: Kill older process with same name

Posted 15 July 2009 - 09:58 AM

	Public Sub EndProcesses(ByVal sProcName As String)
		Dim pProcs() As Process = Process.GetProcessesByName(sProcName)
		If pProcs.Length <> 0 Then
			Dim dLatest As Date = pProcs(0).StartTime
			Dim iIndex As Integer = 0
			Dim i As Integer
			For i = 0 To pProcs.Length - 1
				If pProcs(i).StartTime > dLatest Then
					dLatest = pProcs(i).StartTime
					iIndex = i
				End If
			Next
			For i = 0 To pProcs.Length - 1
				If i <> iIndex Then
					pProcs(i).Kill()
				End If
			Next
		End If
	End Sub
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1