Why is blocking Console.In.ReadLineAsync?

Launch a new console application using the following code -

class Program { static void Main(string[] args) { while (true) { Task<string> readLineTask = Console.In.ReadLineAsync(); Debug.WriteLine("hi"); } } } 

Console.In.ReadLineAsync locks and does not return until a line is entered in the console. therefore, "Hello" is never recorded on the console.

Using wait at Console.In.ReadLineAsync also blocks.

I realized that the new Async CTP methods are not blocking.

What is the reason for this?


Here is another example

 static void Main(string[] args) { Task delayTask = Task.Delay(50000); Debug.WriteLine("hi"); } 

This behaves as I expect, it goes straight to the next line and prints "hello" since Task.Delay is not blocked.

+8
c # async-await
source share
2 answers

daryal answered here http://smellegantcode.wordpress.com/2012/08/28/a-boring-discovery/

It seems that ReadLineAsync does not actually do what it should do. Error in the framework.

My solution is to use ThreadPool.QueueUserWorkItem in a loop so that every call to ReadLineAsync is executed in a new thread.

+5
source share

Now it can be found in the documentation :

Read operations in the standard input stream are performed synchronously. That is, they are blocked until the completion of the specified read operation. This is true even if an asynchronous method, such as ReadLineAsync , is called on the TextReader returned by In .

+2
source share

All Articles