If a given process is running, then end a different process

If a given process is running, then end a different process

Page 1 of 1

5 Replies - 10721 Views - Last Post: 21 September 2009 - 02:10 AM Rate Topic: -----

#1 gmcvey1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-September 09

If a given process is running, then end a different process

Posted 18 September 2009 - 01:37 AM

I hope the title gives a good description, i have read the forum posting tips at the top. Ive just joined the community and nice to meet you all.

Anyways, I am trying to create a vb.net application for which it will check if a process is running in task manager, and if this process or processes are running to end a different process. Let me give you an example,

If processA, ProcessB is running then END ProcessC. If however ProcessA, and ProcessB are not running then ProcessC can stay active. I have started the project and am able to kill the processes etc. This is shown below.


		Dim AProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramA")
		For Each Program As Process In AProcess
		Next

		Dim BProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramB")

		For Each Program As Process In BProcess
		Next

Dim CProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramC")

		For Each Program As Process In CProcess
			Program.Kill()
		Next



Im not sure about where or what direction i need to take this, i hope i have explained it well enough for you to get an idea of what im trying to achieve. Thanks in advance for any help,

Is This A Good Question/Topic? 0
  • +

Replies To: If a given process is running, then end a different process

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: If a given process is running, then end a different process

Posted 18 September 2009 - 03:22 AM

hi gmcvey1

Haven't tested this code but you can give it a try...

		Dim ProcessARunning as Boolean
		Dim ProcessBRunning as Boolean

		Dim AProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramA")
		If Not AProcess Is Nothing Then
		   ProcessARunning = True
		Else
		   ProcessBRunning = False
		End If

		Dim BProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramB")

	   If Not BProcess Is Nothing Then
		   ProcessBRunning = True
		Else
		   ProcessBRunning = False
		End If

		If ProcessARunning = True and ProcessBRunning = True then
		   Dim CProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramC")

		   For Each Program As Process In CProcess
			   Program.Kill()
		   Next
		end if


Was This Post Helpful? 0
  • +
  • -

#3 gmcvey1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-September 09

Re: If a given process is running, then end a different process

Posted 18 September 2009 - 04:41 AM

Many thanks for the sample providied, tried it out.

It does what is required in that it Ends Program C. However even if programA and ProgramB aren't running then it still ends ProgramC anyway. If you catch my drift.


If Not BProcess Is Nothing Then


Am i right in saying this code is based on BProcess = 'Program B' as text which it tries to locate in current process? Im a little unsure about the 'If not' if you would care to explain.

Thank you again for taking time to help,
Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: If a given process is running, then end a different process

Posted 18 September 2009 - 04:53 AM

I was just thinking maybe if the

Dim AProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramA")



returns nothing then you could use the "if is not nothing statement"

Try this it should work

 Dim ProcessARunning as Boolean
		Dim ProcessBRunning as Boolean

		Dim AProcess As Process() = System.Diagnostics.Process.GetProcessesByName("ProgramA")
		If AProcess.count <> 0 Then
		   ProcessARunning = True
		Else
		   ProcessBRunning = False
		End If

		Dim BProcess As Process() = System.Diagnostics.Process.GetProcessesByName("ProgramB")

	   If BProcess.count <> 0 Then
		   ProcessBRunning = True
		Else
		   ProcessBRunning = False
		End If

		If ProcessARunning = True and ProcessBRunning = True then
		   Dim CProcess() As Process = System.Diagnostics.Process.GetProcessesByName("ProgramC")

		   For Each Program As Process In CProcess
			   Program.Kill()
		   Next
		end if



Was This Post Helpful? 1
  • +
  • -

#5 gmcvey1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 18-September 09

Re: If a given process is running, then end a different process

Posted 21 September 2009 - 02:03 AM

Sorry for the delay in reply, im just back at the office.

I have tested the code and it worked successfully which i am very appreciative about. I am going to have this running the backround constantly, i think adding a timer would be the best option for this.

I thread can be closed as it is resolved, hope it is a benefit and helps others.

Thanks again motcom!
Was This Post Helpful? 0
  • +
  • -

#6 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: If a given process is running, then end a different process

Posted 21 September 2009 - 02:10 AM

Cool,


If you use a timer, i suggest you do not make the interval less than 1 sec. (for resource purposes)

(use this post was helpfull :) )
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1