Click here to Skip to main content
15,914,074 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everyone,

I'm trying to add this functionality of checking for open processes and killing it if that is the case. This is what I have so far:
C#
Process[] open_procs = Process.GetProcessesByName("PicoScope.exe");
if (open_procs.Length > 0)
{
   open_procs.Kill();
}

It's not finding the Kill command within the open_procs available commands. Can someone shed some light as to how I can proceed with this?
Posted
Updated 29-Oct-12 8:57am
v3
Comments
Sergey Alexandrovich Kryukov 29-Oct-12 15:42pm    
It's pretty much apparent that you are doing something dirty instead of regular development. No matter how good are the techniques you would use and how good advice you have, you won't get an acceptable resolution of the problem, because your goal is already wrong. This is because you are not addressing to root of the problem at all. And you did not explain your ultimate goal, so you cannot hope for success in this way.
--SA
joshrduncan2012 29-Oct-12 15:46pm    
How can you assume something like that? My intent is that if that the 3rd party process that this program opens is already open, then to kill that process first before launching the new 3rd party process. We don't want to have more than 1 instantiation of the same 3rd party process open at the same time. My program is supposed to add a new menu option to the menu of the 3rd party program. This has nothing to do with anything dirty. What goal are you referring to that you say is "wrong"? I'm not sure I understand that.

1 solution

You are trying to kill the array of processes, rather than the individual processes themselves.
You need to loop through them like this.
C#
Process[] open_procs = Process.GetProcessesByName("PicoScope.exe");
if( open_procs.Length > 0 )
{
   foreach( var proc in open_procs )
   {
      proc.Kill();
   }
}
 
Share this answer
 
v2
Comments
RaisKazi 29-Oct-12 15:33pm    
Simple and accurate. My 5.
fjdiewornncalwe 29-Oct-12 16:10pm    
Thanks, Rais.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900