private static void KillProcessAndChildrens(int pid, AemInstance instance)
{
ManagementObjectSearcher processSearcher = new ManagementObjectSearcher
("Select * From Win32_Process Where ParentProcessID=" + pid);
ManagementObjectCollection processCollection = processSearcher.Get();
try {
Process proc = Process.GetProcessById(pid);
if (proc != null && !proc.HasExited) {
mLog.Debug("Killing process #" + pid + " for instance " + instance.Name);
proc.Kill();
}
}
catch (ArgumentException) {
// Process already exited.
}
if (processCollection != null) {
foreach (ManagementObject mo in processCollection) {
// kill child processes(also kills childrens of childrens etc.)
KillProcessAndChildrens(Convert.ToInt32(mo["ProcessID"]), instance);
}
}
}