omriman12 omriman12 - 2 months ago 47
C# Question

System.Diagnostics.Process WaitForExit doesnt kill the process on timeout

I run

from my c# code:

ProcessStartInfo processStartInfo = new ProcessStartInfo
WindowStyle = ProcessWindowStyle.Hidden,
FileName = zipFileExePath,
Arguments = "x \"" + zipPath + "\" -o\"" + extractPath + "\""

using (Process process = Process.Start(processStartInfo))
if (process.WaitForExit(timeout))
s_logger.ErrorFormat("Timeout whilte extracting extracting {0}", zipPath);


Now what I see happening is that when the timeout hits, still the
process exists in
Task Manager
. Why is that happening? I put both close and dispose

Answer Source

Your question mentions three methods on the Process class:

None of those methods will do anything to the process, certainly not kill it.

WaitForExit is an observational method, it will wait and see if the process terminates by itself. The overload with the timeout will return a bool telling you whether the method observed that the process terminated or not. If it didn't, it's presumably still running.

Close and Dispose does nothing to or for the process that is running, it merely closes the handle you have internally in the Process class that is the mechanism for how that class can keep tabs of the process in question.

It's a good idea to close this handle when you're done observing the running process.

However, if you want the process to terminate, you will have to call Process.Kill.