Skip to content

Instantly share code, notes, and snippets.

@richlander
Last active January 29, 2021 12:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richlander/251eff3ce9025df0f2fdb4b7f69aa250 to your computer and use it in GitHub Desktop.
Save richlander/251eff3ce9025df0f2fdb4b7f69aa250 to your computer and use it in GitHub Desktop.
Wait for user cancel
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