← All source files

V14 / fad4e1a110e5

ProcessExtensions.cs

Lilith/Core/Extensions/ProcessExtensions.cs

Unchanged from the preceding 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);
        }
    }
}