I wrote the following C# code in .NET 6 in a simple console application. It captures "Interrupt" signal (i.e. Ctrl+C) and signals cancellation token source to cancel its token.
Then in Main
method, I want the thread to exit. Why does the following code not work as expected i.e. the Main
method does not exit and I don't see the message "Exiting..."
namespace ConsoleApp1
{
public class Program
{
private static CancellationTokenSource cancellationTokenSource =
new CancellationTokenSource();
public static void Main(string[] args)
{
Console.CancelKeyPress += new ConsoleCancelEventHandler(InterruptHandler);
Task.Delay(Timeout.Infinite, cancellationTokenSource.Token).Wait();
Console.WriteLine("Exiting...");
}
private static void InterruptHandler(object? sender, ConsoleCancelEventArgs e)
{
cancellationTokenSource.Cancel();
}
}
}
Then I changed the main method to this, still same thing happened.
public static void Main(string[] args)
{
Console.CancelKeyPress += new ConsoleCancelEventHandler(InterruptHandler);
while (cancellationTokenSource.IsCancellationRequested == false)
Thread.Sleep(1000);
Console.WriteLine("Exiting...");
}
As I found out, Ctrl+C terminates the process, so your app just ends after pressing the combination. In order to change that behaviour you need to set
ConsoleCancelEventArgs.Cancel
property totrue
.This will change behaviour, but it won't be exactly as you expect, as cancelling task is just interrupting it by throwing exception, so you'd need to add handling of that exception, please see below example:
UPDATE As suggested, you could simplify the code by replacing
Wait()
method withGetAwaiter().GetResult()
, which would throwTaskCancelledException
instead ofAggregateException
: