2 Replies - 3834 Views - Last Post: 15 October 2010 - 03:08 AM

#1 PsychoCoder   User is offline

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

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

Check if a process is running

Posted 21 December 2007 - 12:26 PM

Description: Pass the method a process name (minus the .EXE)This is a snippet I use often to check and see if a certain process is running. It loops through all the processes in your Task Manager looking for the name your provide it
//Namespaces we need to use
using System.Diagnostics;


public bool IsProcessOpen(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. 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.
		//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
		if (clsProcess.ProcessName.Contains(name))
		{
			//if the process is found to be running then we
			//return a true
			return true;
		}
	}
	//otherwise we return a false
	return false;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Check if a process is running

#2 PsychoCoder   User is offline

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

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

Re: Check if a process is running

Posted 21 December 2007 - 12:26 PM

Description: Pass the method a process name (minus the .EXE)This is a snippet I use often to check and see if a certain process is running. It loops through all the processes in your Task Manager looking for the name your provide it
//Namespaces we need to use
using System.Diagnostics;

public bool IsProcessOpen(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. 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.
		//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
		if (clsProcess.ProcessName.Contains(name))
		{
			//if the process is found to be running then we
			//return a true
			return true;
		}
	}
	//otherwise we return a false
	return false;
}

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

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

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

Re: Check if a process is running

Posted 21 December 2007 - 12:26 PM

Description: Pass the method a process name (minus the .EXE)This is a snippet I use often to check and see if a certain process is running. It loops through all the processes in your Task Manager looking for the name your provide it
//Namespaces we need to use
using System.Diagnostics;

public bool IsProcessOpen(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. 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.
		//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
		if (clsProcess.ProcessName.Contains(name))
		{
			//if the process is found to be running then we
			//return a true
			return true;
		}
	}
	//otherwise we return a false
	return false;
}

Was This Post Helpful? 0
  • +
  • -

#4 ankit_virgo239   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 27-July 09

Re: Check if a process is running

Posted 27 July 2009 - 04:05 AM

hi its giving error Foreach cannot operate on a 'method group'. Did you intend to invoke the 'method group'? and where to list the process v intend to check. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 kudresov   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 15-October 10

Re: Check if a process is running

Posted 15 October 2010 - 03:08 AM

Or this can be simplified to one line (.NET 3.5) Process.GetProcesses().Any(prc => prc.ProcessName.Contains(name));
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1