Kill process with its children

pull/18/head
toruuetani 2013-06-21 22:43:45 +09:00
parent 314fb7b9d0
commit c50bb61b7b
1 changed files with 21 additions and 1 deletions

22
Main.cs
View File

@ -13,6 +13,7 @@ using System.Xml;
using System.Threading;
using Microsoft.Win32;
using Advapi32;
using System.Management;
namespace winsw
{
@ -291,7 +292,7 @@ namespace winsw
try
{
WriteEvent("ProcessKill " + process.Id);
process.Kill();
StopProcessAndChildren(process.Id);
}
catch (InvalidOperationException)
{
@ -328,6 +329,25 @@ namespace winsw
WriteEvent("Finished " + descriptor.Id);
}
private void StopProcessAndChildren(int pid)
{
var searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid);
foreach (var mo in searcher.Get())
{
StopProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
try
{
var proc = Process.GetProcessById(pid);
proc.Kill();
}
catch (ArgumentException)
{
// Process already exited.
}
}
private void WaitForProcessToExit(Process process)
{
SignalShutdownPending();