Process.Kill returns System.ComponentModel.Win32Exception

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All!

I've run into this problem and I would like you to help me please.

Before run my app, I need to iterate through the system process list and kill some processes that consume a lot of CPU, just to free memory:

Dim mProc As Process
For Each mProc In System.Diagnostics.Process.GetProcesses()
If InStr(mProc.ProcessName, "P1", CompareMethod.Text) > 0 Then mProc.Kill()
If InStr(mProc.ProcessName, "P2", CompareMethod.Text) > 0 Then mProc.Kill()
If InStr(mProc.ProcessName, "P3", CompareMethod.Text) > 0 Then mProc.Kill()
Next

The problem is that I got this exception with some of them:

System.ComponentModel.Win32Exception: Access Denied

The same happens when I try to change their PriorityClass property. All I know is that these processes are started by a Windows Service, it seems that they are kind of protected ??? Maybe I can stop the service directly instead (if I know how to do it of course :-) )

Does anybody have any idea to work around this?? I'm new in .NET...

Thanks in advance,
Fernando Paes
 
* "=?Utf-8?B?RmVybmFuZG8gUGFlcw==?= said:
I've run into this problem and I would like you to help me please.

Before run my app, I need to iterate through the system process list and kill some processes that consume a lot of CPU, just to free memory:

Dim mProc As Process
For Each mProc In System.Diagnostics.Process.GetProcesses()
If InStr(mProc.ProcessName, "P1", CompareMethod.Text) > 0 Then mProc.Kill()
If InStr(mProc.ProcessName, "P2", CompareMethod.Text) > 0 Then mProc.Kill()
If InStr(mProc.ProcessName, "P3", CompareMethod.Text) > 0 Then mProc.Kill()
Next

Notice that you cannot change a collection while looping through it
using 'For...Each'. Imagine, the process is already killed by your
first 'If...Then...', and the 2nd 'If...Then...' condition will be
'True' too...
The problem is that I got this exception with some of them:

System.ComponentModel.Win32Exception: Access Denied

Are you running the app with administrator rights?
 
Herfried,
Notice that you cannot change a collection while looping through it
using 'For...Each'.
Remember that GetProcesses returns an array of Processes, instead of an
IEnumerator object itself. So in this case its safe to delete the process
object while iterating the collection.

However! I agree, you normally should not attempting to change a collection
while looping through it using "for each".

Jay
 
* "Jay B. Harlow said:
Remember that GetProcesses returns an array of Processes, instead of an
IEnumerator object itself. So in this case its safe to delete the process
object while iterating the collection.

Ooops. You are right :-).
 
* "=?Utf-8?B?RmVybmFuZG8gUGFlcw==?= said:
I think my post appeared twice! Hope some Microsoft's fix it...

That's "impossible", but don't worry about that.

I saw that Jay already gave you an answer about how to stop a service.
 
Yes, now I got it with:

Dim mService As ServiceController

For Each mService In ServiceProcess.ServiceController.GetServices
If InStr(mService.ServiceName, "myService", CompareMethod.Text) > 0 Then
mService.Stop()
mService.WaitForStatus(ServiceControllerStatus.Stopped, System.TimeSpan.FromMinutes(1))
Next

Thanks,
Fernando Paes
 
Back
Top