However, when calling Console.GetCursorPosition at least twice in the same iteration of a for loop in which thread A also calls Console.ReadLine, this behavior no longer occurs and from then on B is able to call Console.GetCursorPosition without being blocked.
Reproduction Steps
Create a new console project, targeting .NET 8 or .NET 9.
Run the following:
static void Main(string[] args)
{
Task.Run(() =>
{
while (true)
{
Console.WriteLine($"CursorPosition = {Console.GetCursorPosition()}");
Thread.Sleep(1000);
}
});
for (int i = 0; i < 10; i++)
{
string? input = Console.ReadLine();
if (i == 2)
{
Console.GetCursorPosition();
Console.GetCursorPosition();
}
}
}
Expected behavior
"CursorPosition = ..." should be printed every second, or remain blocked until the calls to Console.ReadLine() complete.
Actual behavior
In each iteration of the loop, nothing is printed until a string is read from Console.ReadLine(). However, once Console.GetCursorPosition() is called twice in the same iteration of the loop, the "CursorPosition = ..." line is printed every second as expected.
For example, CursorPosition = ... is only outputted after a new line is inputted, but after the GetCursorPosition lines are executed, the output continues without anything having been entered:
Moving one of the GetCursorPosition calls before the Console.ReadLine produces the same result as when both calls are done after the Console.ReadLine:
for (int i = 0; i < 10; i++)
{
Console.GetCursorPosition();
string? input = Console.ReadLine();
if (i == 2)
Console.GetCursorPosition();
}
Note the i == 2 can be any number (and the loop max is irrelevant), this is only to show behavior is consistent before GetCursorPosition is called a second time.
Regression?
No response
Known Workarounds
No response
Configuration
Occurs in both .NET 8.0.403 and 9.0.100-preview.7.24407.12
Running Ubuntu 22.04 via WSL2 on Windows 11.
Same behavior in debug and release builds
Outside of WSL, the task outputs every second as expected on both .NET versions.
Description
Console.GetCursorPosition
from thread B can/will be blocked until a call toConsole.ReadLine
in thread A completes.Console.GetCursorPosition
at least twice in the same iteration of afor
loop in which thread A also callsConsole.ReadLine
, this behavior no longer occurs and from then on B is able to callConsole.GetCursorPosition
without being blocked.Reproduction Steps
Expected behavior
"CursorPosition = ..." should be printed every second, or remain blocked until the calls to Console.ReadLine() complete.
Actual behavior
In each iteration of the loop, nothing is printed until a string is read from
Console.ReadLine()
. However, onceConsole.GetCursorPosition()
is called twice in the same iteration of the loop, the "CursorPosition = ..." line is printed every second as expected.For example,
CursorPosition = ...
is only outputted after a new line is inputted, but after theGetCursorPosition
lines are executed, the output continues without anything having been entered:Moving one of the
GetCursorPosition
calls before theConsole.ReadLine
produces the same result as when both calls are done after theConsole.ReadLine
:Note the
i == 2
can be any number (and the loop max is irrelevant), this is only to show behavior is consistent beforeGetCursorPosition
is called a second time.Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response