Created
September 8, 2020 11:04
-
-
Save rstropek/04db9f814d7a0e6f7aaaef8693ceb9d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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