Skip to content

Instantly share code, notes, and snippets.

View BenMakesGames's full-sized avatar

Ben Hendel-Doying BenMakesGames

View GitHub Profile
@AArnott
AArnott / Cancellation.cs
Last active December 12, 2023 16:33
Graceful console app cancellation on Ctrl+C
class Program
{
static async Task Main(string[] args)
{
// Add this to your C# console app's Main method to give yourself
// a CancellationToken that is canceled when the user hits Ctrl+C.
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (s, e) =>
{
Console.WriteLine("Canceling...");