0

I have a Dotnet Core API application that is published as an .EXE

When I run the application (double click the .EXE), the console window opens, but nothing happens (the console window remains black - no text is shown, the application does not start)

This only occurs one in so many times

Only after I press a random key on my keyboard (while the console window is focussed), the application starts writing output to the console and the application starts as expected

It seems it like the application is "waiting" or "halting" and only continues to run after any key is pressed

It only happens sometimes, not every time, which makes it hard for me to figure out the cause

Has anyone else had this issue? What can I do to prevent this behaviour?

THEoneANDonly
  • 382
  • 1
  • 2
  • 16

1 Answers1

1

You can reproduce this behaviour like that:

for(int i = 0; i < 10000; i++)
{ 
 System.Console.WriteLine($"{i}");
 System.Threading.Thread.Sleep(100);
}

As soon as you click into the output window, or scroll, it will stop.

Why does it stop?

Console.WriteLine is your issue here. It will block until the output is written, as it calls the Write method of the underlying stream instance.

So, when you run a dotnet console application that writes to the console, and, for example, scroll in its output, or click somewhere into the output window, it will halt at the next Console.WriteLine, until you scroll right down to the bottom again, press a key or whatnot.

That behaviour is (I think) considered a feature by most people, including me, as its annoying to scroll up and try to read something, while the line you want to read keeps slipping away from your eyes.

How do I prevent that?

You can look at this question to find out how to solve that, it basically involves writing an asynchronous console, or using a proper logging framework.