Skip to content

Commit

Permalink
Kill process and it's childrens
Browse files Browse the repository at this point in the history
  • Loading branch information
DareFox committed Feb 14, 2022
1 parent 958c314 commit 1c9a167
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion IdleSharedLib/AppRunner.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Management;

namespace IdleSharedLib
{
Expand Down Expand Up @@ -43,7 +44,7 @@ public void killAll()
try
{
Logger.Log($"Trying to kill ${item.ProcessName} (id: {item.Id})");
item.Kill();
KillProcessAndChildren(item.Id);
}
catch (Exception ex)
{
Expand All @@ -54,5 +55,25 @@ public void killAll()
Logger.Log("All processes are killed");
processes.Clear();
}

private static void KillProcessAndChildren(int pid)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher
("Select * From Win32_Process Where ParentProcessID=" + pid);
ManagementObjectCollection moc = searcher.Get();
foreach (ManagementObject mo in moc)
{
KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"]));
}
try
{
Process proc = Process.GetProcessById(pid);
proc.Kill();
}
catch (ArgumentException)
{
// Process already exited.
}
}
}
}

0 comments on commit 1c9a167

Please sign in to comment.