← All source files

V1 / 8f6c47b125e8

ProcessExtensions.cs

Lilith/Core/Extensions/ProcessExtensions.cs

Changed in this version
CS
using System.Diagnostics;

namespace Lilith.Core.Extensions;

public static class ProcessExtensions
{
    /// <summary>On timeout, terminates the process tree and waits before releasing ownership.</summary>
    public static async Task WaitForExitOrKillAsync(this Process process, TimeSpan timeout, string message)
    {
        try { await process.WaitForExitAsync().WaitAsync(timeout); }
        catch (TimeoutException)
        {
            if (!process.HasExited) process.Kill(entireProcessTree: true);
            await process.WaitForExitAsync();
            throw new TimeoutException(message);
        }
    }
}