Process.Kill() problem

R

Rick

Hi,

I'm having a problem using Process.Kill(). I've been getting the
Win32Exception "The associated process could not be terminated". I'm using
OpenNETCF SmartDeviceFramework 1.3. I know that there was a bug in
Process.Kill() before but I assume this has been fixed with the latest
version.

I'm awaiting an executable that I will start and kill so in the mean time I'm
just starting iexplore.exe and trying to kill it to get me going. I'm
starting the process with "psi.UseShellExecute = false;" to give me a valid
PID. I noticed that I could kill a different process, ceplayer.exe. Why can
I kill ceplayer.exe but not iexplore.exe?

I tried a work around recommended by Peter Foot. I P/Invoke
TerminateProcess() and CloseHandle() but this didn't work either.
TerminateProcess always returns failure and the process does not die. I pass
TerminateProcess a value of 1 for exitCode which I got from a call to
GetExitCodeProcess().

Please let me know if I'm doing something wrong. Thank you.

Rick
 
P

Peter Foot [MVP]

Can you catch the Win32Exception and read it's NativeErrorCode property -
this should help pin down the cause of the exception - the return value from
TerminateProcess.

Peter
 
R

Rick

Peter,

Thanks for your response. Sorry for the delay, I got busy with other stuff
at work. When Process.Kill() throws the Win32Exception the NativeErrorCode
is 0x6, "Invalid Handle". I was dumping the handle and it always looks valid
to me. When I use the alternative TerminateProcess() it just returns false
(failure) with no exception.

I think the iexplore.exe cannot be killed for some reason. There may be
subprocesses or parent processes associated with it so Windows doesn't allow
it to be killed? I don't know but that's what I'm speculating. Utilimately
iexplore.exe isn't what I need to kill anyways. When the program I need
becomes available I'll need to kill that and hopefully that won't give me
this problem.

Rick
 
P

Paul G. Tobey [eMVP]

The handle is bad. You can't tell by looking at it if it's a valid handle
or not; it's just a 32-bit integer, basically.

You *can* kill Internet Explorer (try the Remote Process Viewer and you can
verify that). Using TerminateProcess() to kill applications is the wrong
way to do almost anything. Don't do this unless you are a process
management utility (like Remote Process Viewer), or something of that sort.
It is a very bad idea to design your system on the assumption that you're
going to terminate some process in this way.

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top