mirror of https://github.com/winsw/winsw
Kill process with its children
parent
314fb7b9d0
commit
c50bb61b7b
22
Main.cs
22
Main.cs
|
@ -13,6 +13,7 @@ using System.Xml;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
using Advapi32;
|
using Advapi32;
|
||||||
|
using System.Management;
|
||||||
|
|
||||||
namespace winsw
|
namespace winsw
|
||||||
{
|
{
|
||||||
|
@ -291,7 +292,7 @@ namespace winsw
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
WriteEvent("ProcessKill " + process.Id);
|
WriteEvent("ProcessKill " + process.Id);
|
||||||
process.Kill();
|
StopProcessAndChildren(process.Id);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException)
|
||||||
{
|
{
|
||||||
|
@ -328,6 +329,25 @@ namespace winsw
|
||||||
WriteEvent("Finished " + descriptor.Id);
|
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)
|
private void WaitForProcessToExit(Process process)
|
||||||
{
|
{
|
||||||
SignalShutdownPending();
|
SignalShutdownPending();
|
||||||
|
|
Loading…
Reference in New Issue