7

I have a very simple console app.

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

Here DoAsync starts set of task and returns not waiting for tasks' completition. Each task writes to Console, but the ouptut is not shown before key is pressed.
When I use Console.ReadLine everything works fine.

So I'm curious about ReadKey() pecularities.

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137

1 Answers1

10

From the documentation for Console.ReadKey():

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

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

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

Reading this article I'm guessing you have .NET 4.5 installed?

greg84
  • 7,541
  • 3
  • 36
  • 45
  • Many-many thanks, I was looking one level higher (http://msdn.microsoft.com/en-us/library/w19esh7k.aspx) And for the article too. – Pavel Voronin Apr 01 '13 at 13:12