Last active
January 14, 2021 03:18
-
-
Save NickStrupat/0c68708a14ecdf366702f111a2f62020 to your computer and use it in GitHub Desktop.
This file contains 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; | |
static class AsyncProgram | |
{ | |
private static readonly CancellationTokenSource cts = new CancellationTokenSource(); | |
public static CancellationToken CancellationToken => cts.Token; | |
static AsyncProgram() | |
{ | |
Console.CancelKeyPress += EventHandler; | |
AppDomain.CurrentDomain.ProcessExit += EventHandler; | |
static void EventHandler(Object? sender, EventArgs e) | |
{ | |
if (cts.IsCancellationRequested) | |
return; | |
cts.Cancel(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment