-
-
Save richlander/251eff3ce9025df0f2fdb4b7f69aa250 to your computer and use it in GitHub Desktop.
Wait for user cancel
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; | |
using static System.Console; | |
using CancellationTokenSource cts = new(); | |
CancellationToken token = cts.Token; | |
Console.CancelKeyPress += (s, e) => | |
{ | |
e.Cancel = true; | |
cts.Cancel(); | |
}; | |
while(!token.WaitHandle.WaitOne(1000)) | |
{ | |
WriteLine("Doing loop work"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment