© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•7mo ago•
2 replies
jepperen

Console.In.ReadLineAsync(CancellationToken)

Anyone know a good fix for this? ReadLineAsync is not being cancelled until I feed my terminal a newline character
var cts = new CancellationTokenSource();

Console.CancelKeyPress += (s, e) =>
{
    Console.WriteLine("Cancelling!");
    cts.Cancel();
    e.Cancel = true;
};

Console.WriteLine("Enter commands...");
while (!cts.IsCancellationRequested)
{
    await Console.In.ReadLineAsync(cts.Token).ConfigureAwait(false);
}
Console.WriteLine("Bye...");
var cts = new CancellationTokenSource();

Console.CancelKeyPress += (s, e) =>
{
    Console.WriteLine("Cancelling!");
    cts.Cancel();
    e.Cancel = true;
};

Console.WriteLine("Enter commands...");
while (!cts.IsCancellationRequested)
{
    await Console.In.ReadLineAsync(cts.Token).ConfigureAwait(false);
}
Console.WriteLine("Bye...");
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Queue Cancellationtoken
C#CC# / help
2y ago
CancellationToken not cancelling
C#CC# / help
15mo ago
❔ HttpClient does not respect CancellationToken
C#CC# / help
3y ago
Unable to cancel async operation using CancellationToken
C#CC# / help
3y ago