Skip to content

Instantly share code, notes, and snippets.

@rstropek
Created September 8, 2020 11:04
Show Gist options
  • Save rstropek/04db9f814d7a0e6f7aaaef8693ceb9d7 to your computer and use it in GitHub Desktop.
Save rstropek/04db9f814d7a0e6f7aaaef8693ceb9d7 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
var tcs = new TaskCompletionSource();
var sigintReceived = false;
Console.WriteLine("Waiting for SIGINT/SIGTERM");
Console.CancelKeyPress += (_, ea) =>
{
// Tell .NET to not terminate the process
ea.Cancel = true;
Console.WriteLine("Received SIGINT (Ctrl+C)");
tcs.SetResult();
sigintReceived = true;
};
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
if (!sigintReceived)
{
Console.WriteLine("Received SIGTERM");
tcs.SetResult();
}
else
{
Console.WriteLine("Received SIGTERM, ignoring it because already processed SIGINT");
}
};
await tcs.Task;
Console.WriteLine("Good bye");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment