2024-10-29 01:27:51 -05:00
|
|
|
|
using LANCommander.SDK.Helpers;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.SDK.Extensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static class ProcessExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
public static async Task WaitForAllExitAsync(this Process parentProcess, CancellationToken cancellationToken = default)
|
|
|
|
|
|
{
|
|
|
|
|
|
var exited = parentProcess.HasExited;
|
|
|
|
|
|
int pid = parentProcess.Id;
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerable<int> existingProcessIds = Process.GetProcesses().Select(p => p.Id);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await parentProcess.WaitForExitAsync();
|
|
|
|
|
|
|
2025-07-30 19:29:18 -05:00
|
|
|
|
IEnumerable<int> newProcessIds = Process.GetProcesses().Where(p => !existingProcessIds.Contains(p.Id)).Select(p => p.Id);
|
2024-10-29 01:27:51 -05:00
|
|
|
|
|
|
|
|
|
|
foreach (var processId in newProcessIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
|
|
// This could be adapted better for non-Windows platforms.
|
|
|
|
|
|
// Not even sure how Process works on other platforms, but
|
|
|
|
|
|
// there's other ways of tracking child processes. One way
|
|
|
|
|
|
// that would need to be tested out would be to get the
|
|
|
|
|
|
// child process at this point and check the executable's
|
|
|
|
|
|
// file location. If it's in {InstallDir}, track it!
|
|
|
|
|
|
int? parentProcessId = ProcessHelper.GetParentProcessId(processId);
|
|
|
|
|
|
|
|
|
|
|
|
if (parentProcessId == pid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var process = Process.GetProcessById(processId);
|
|
|
|
|
|
|
|
|
|
|
|
if (!process.HasExited)
|
|
|
|
|
|
{
|
2025-07-30 19:29:18 -05:00
|
|
|
|
|
2024-10-29 01:27:51 -05:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await process.WaitForExitAsync(cancellationToken);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
|
{
|
2024-10-29 18:24:01 -05:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!process.HasExited)
|
|
|
|
|
|
process.Kill();
|
|
|
|
|
|
});
|
2024-10-29 01:27:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
|
{
|
2025-07-30 19:29:18 -05:00
|
|
|
|
var childProcessIds = Process.GetProcesses().Where(p => ProcessHelper.GetParentProcessId(p.Id) == pid).Select(p => p.Id);
|
2024-10-29 01:27:51 -05:00
|
|
|
|
|
|
|
|
|
|
foreach (var childProcessId in childProcessIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var childProcess = Process.GetProcessById(childProcessId);
|
|
|
|
|
|
|
2024-10-29 18:24:01 -05:00
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!childProcess.HasExited)
|
|
|
|
|
|
childProcess.Kill();
|
|
|
|
|
|
});
|
2024-10-29 01:27:51 -05:00
|
|
|
|
}
|
2025-07-30 19:29:18 -05:00
|
|
|
|
catch { }
|
2024-10-29 01:27:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-29 18:24:01 -05:00
|
|
|
|
await Task.Run(() =>
|
2024-10-29 01:27:51 -05:00
|
|
|
|
{
|
2024-10-29 18:24:01 -05:00
|
|
|
|
if (!parentProcess.HasExited)
|
|
|
|
|
|
parentProcess.Kill();
|
|
|
|
|
|
});
|
2024-10-29 01:27:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|