6 Replies - 1143 Views - Last Post: 12 September 2011 - 05:34 AM

#1 PsychoCoder   User is offline

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

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

Kill a process if it's running

Posted 21 December 2007 - 01:46 PM

Description: Pass the method the name of the process you want to kill (minus the .EXE)This is a snippet I use to search for and kill a process if it is found in the list of running processes.
//Namespaces needed
using System.Diagnostics;

public bool FindAndKillProcess(string name)
{
	//here we're going to get a list of all running processes on
	//the computer
	foreach (Process clsProcess in Process.GetProcesses()) {
		//now we're going to see if any of the running processes
		//match the currently running processes by using the StartsWith Method,
		//this prevents us from incluing the .EXE for the process we're looking for.
		//. Be sure to not
		//add the .exe to the name you provide, i.e: NOTEPAD,
		//not NOTEPAD.EXE or false is always returned even if
		//notepad is running
		if (clsProcess.ProcessName.StartsWith(name))
		{
			//since we found the proccess we now need to use the
			//Kill Method to kill the process. Remember, if you have
			//the process running more than once, say IE open 4
			//times the loop thr way it is now will close all 4,
			//if you want it to just close the first one it finds
			//then add a return; after the Kill
			clsProcess.Kill();
			//process killed, return true
			return true;
		}
	}
	//process not found, return false
	return false;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Kill a process if it's running

#2 Korupt   User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 185
  • Joined: 22-June 08

Re: Kill a process if it's running

Posted 15 August 2008 - 01:22 PM

"Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?" I get this compiler error
Was This Post Helpful? 0
  • +
  • -

#3 Dykam   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-September 08

Re: Kill a process if it's running

Posted 14 September 2008 - 12:02 AM

He need's to change foreach (Process clsProcess in Process.GetProcesses) { to foreach (Process clsProcess in Process.GetProcesses()) {
Was This Post Helpful? 0
  • +
  • -

#4 salahuddincse   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 05-March 09

Re: Kill a process if it's running

Posted 05 March 2009 - 09:18 PM

good
Was This Post Helpful? 0
  • +
  • -

#5 simontasker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 06-March 09

Re: Kill a process if it's running

Posted 06 March 2009 - 06:49 AM

Hi, this is a great little snippet of code, im just curious how you would reference/call it in your main code? is the it similar to a function? Thanks
Was This Post Helpful? 0
  • +
  • -

#6 bshoemaker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 06-March 09

Re: Kill a process if it's running

Posted 06 March 2009 - 03:50 PM

In addition, he also left out the fact that if the process is a system process, a Win32Exception will be hit, crashing the program. The clsProcess.Kill() code should be within a try/catch block catching Win32Exceptions.
Was This Post Helpful? 0
  • +
  • -

#7 RumeshChanchal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 12-September 11

Re: Kill a process if it's running

Posted 12 September 2011 - 05:34 AM

great!!!!!!! check out the following links....... http://www.mindstick...0with%20Example
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1