Skip to content

Commit

Permalink
Check for parent process when killing off rclone to avoid killing off…
Browse files Browse the repository at this point in the history
… unrelated rclone processes.
  • Loading branch information
frxctura committed May 23, 2023
1 parent dd1044e commit 32ea388
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions RCLONE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Text;
using System.Windows.Forms;

Expand All @@ -12,9 +13,28 @@ internal class RCLONE
//Kill all rclone, using a static rclone variable doesn't work for some reason #tofix
public static void killRclone()
{
foreach (var process in Process.GetProcessesByName("rclone"))
var parentProcessId = Process.GetCurrentProcess().Id;
var processes = Process.GetProcessesByName("rclone");

foreach (var process in processes)
{
process.Kill();
try
{
using (ManagementObject obj = new ManagementObject($"win32_process.handle='{process.Id}'"))
{
obj.Get();
var ppid = Convert.ToInt32(obj["ParentProcessId"]);

if (ppid == parentProcessId)
{
process.Kill();
}
}
}
catch (Exception ex)
{
// Handle exception if the process no longer exists
}
}
}

Expand Down

0 comments on commit 32ea388

Please sign in to comment.