Question Details

No question body available.

Tags

c# async-await console-application

Answers (1)

February 10, 2026 Score: 6 Rep: 30,686 Quality: High Completeness: 60%

Console.In.ReadLineAsync is actually a pseudo asynchronous method, it still blocks the current thread just like Console.ReadLine causing Task.WaitAny to not execute at all.

You can create such a task to avoid it blocking the thread:

static Task TaskInput() => Task.Run(Console.ReadLine);

Besides, you code may still have an error. You should want only one ReadLine to be executed at the same time, but now each loop will result in a wait for Enter +1. This input task needs to be recreated at least after it is completed:

Task inputTask = TaskInput(); while (!quit) { string? inputResult = null; var completedTask = await Task.WhenAny(inputTask, backgroundTcs.Task); if (completedTask == inputTask) { inputResult = inputTask.Result; inputTask = TaskInput(); } Console.WriteLine($"Status: {inputTask.Status}, {backgroundTcs.Task.Status}"); // do something with either of the tasks, depending on their IsCompleted properties if (inputResult != null) { quit = DispatchDebuggerCommand(inputResult); } }