Why does Console.ReadKey () output Console.WriteLine output to another thread?

I have a very simple console application.

static void Main(string[] args) { DoAsync(); Console.ReadKey(); } 

Here, DoAsync starts the task set and returns without waiting for the task to complete. Each task is written to the Console, but the output is not displayed until the key is pressed.
When I use Console.ReadLine , everything works fine.

Therefore, I am interested to learn about the features of ReadKey() .

+6
source share
1 answer

From the documentation for Console.ReadKey() :

The ReadKey method waits, that is, blocks the thread issuing the ReadKey until a key or function key is pressed.

What he actually does is get a lock on Console.InternalSyncObject , which prevents further operations on the console.

The Console.ReadLine() method does not block the stream in this way.

Reading this article I assume you have .NET 4.5 installed?

+8
source

All Articles