I have the following code:
foreach (var process in Process.GetProcessesByName(name))
{
try
{
process.Kill();
if (!process.WaitForExit(timeout))
{
session.Log("Killing process {0} timed out.", name);
}
else
{
session.Log("Killing process {0} finished successfully.", name);
}
}
catch (Exception exception)
{
session.Log("Exception while trying to kill process {0}: {1}", name, exception.ToString());
}
}
Process.Kill is throwing the following exception:
Exception while trying to kill process <process name>: System.ComponentModel.Win32Exception (0x80004005): Access is denied
at System.Diagnostics.Process.Kill()
at CustomActions.CustomActions.KillAllProcesses(String name, Session session, Nullable`1 wait)
The code usually works, but the odd time it does fail. Its part of a custom action on an installer that I have built, and when it runs it runs under the local system account. I have seen this happen with Process.Kill in the past. I know there are some alternatives to it. I cannot modify the code of the process in question so I am on my own to terminate it however I see fit. What is the best method of nuking a process that needs to die ASAP if I don't care about leaving it in some intermediate state because I am uninstalling its files from the system? Any IO should be allowed to finish or get canceled in case any settings are in the process of being set for the app on a global level and the user does an upgrade. Would P/Invoking TerminateProcess be the best solution?