You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.2 KiB
42 lines
1.2 KiB
namespace Recyclarr.Cli.Console.Helpers;
|
|
|
|
// Taken from: https://github.com/spectreconsole/spectre.console/issues/701#issuecomment-1081834778
|
|
internal sealed class ConsoleAppCancellationTokenSource
|
|
{
|
|
private readonly CancellationTokenSource _cts = new();
|
|
|
|
public CancellationToken Token => _cts.Token;
|
|
|
|
public ConsoleAppCancellationTokenSource()
|
|
{
|
|
System.Console.CancelKeyPress += OnCancelKeyPress;
|
|
AppDomain.CurrentDomain.ProcessExit += OnProcessExit;
|
|
|
|
using var _ = _cts.Token.Register(() =>
|
|
{
|
|
AppDomain.CurrentDomain.ProcessExit -= OnProcessExit;
|
|
System.Console.CancelKeyPress -= OnCancelKeyPress;
|
|
});
|
|
}
|
|
|
|
private void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e)
|
|
{
|
|
// NOTE: cancel event, don't terminate the process
|
|
e.Cancel = true;
|
|
|
|
_cts.Cancel();
|
|
}
|
|
|
|
private void OnProcessExit(object? sender, EventArgs e)
|
|
{
|
|
if (_cts.IsCancellationRequested)
|
|
{
|
|
// NOTE: SIGINT (cancel key was pressed, this shouldn't ever actually hit however, as we remove the event
|
|
// handler upon cancellation of the `cancellationSource`)
|
|
return;
|
|
}
|
|
|
|
_cts.Cancel();
|
|
}
|
|
}
|