Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Console.WriteLine before allocating the console

I've recently encountered the following problem with my application: it didn't show any console output, though the console had been allocated by using AllocConsole. I managed to figure out soon that it was caused by an attempt (hidden deeply in code) to write to the console before the AllocConsole was called. So it looked like this:

Console.WriteLine("Foo"); // no console allocated yet
AllocConsole();           // console window appears
Console.WriteLine("Bar"); // expecting "Bar" in the console, but the console is blank

So my question is: why does this happen? I don't see any exceptions (though I suppose they are there).

like image 305
Dmitrii Erokhin Avatar asked Sep 24 '11 05:09

Dmitrii Erokhin


People also ask

What is use of console WriteLine () method?

Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.

What is the difference between console WriteLine () and console ReadLine ()?

It also redirects the standard output from the console to the output file. It then uses the Console. ReadLine method to read each line in the file, replaces every sequence of four spaces with a tab character, and uses the Console. WriteLine method to write the result to the output file.

What is the difference between console WriteLine and console write?

While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.

How do I get console WriteLine output in Visual Studio?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.


2 Answers

The first time you use Console.WriteLine, the Console class creates a TextWriter and associates it with the Console.Out property. The way it does this is to use Win32 to open the low-level file handle associated with the standard output file handle. If the standard output handle is invalid, Console.Out is set to TextWriter.Null, which discards all output.

The Win32 AllocConsole function, creates and sets the standard output handle so after calling it the standard output handle is either different or now valid. In either case, Console.Out has already been set either to use the old standard output or to discard all output.

To force a re-open of Console.Out after calling AllocConsole, you can use this method:

  • Console.OpenStandardOutput
like image 178
Rick Sladkey Avatar answered Sep 21 '22 13:09

Rick Sladkey


Probably because the static constructor of the Console class sets up the output stream the first time you call Console.WriteLine. Since there's no console attached, and therefore no standard output handle, output gets routed to the bit bucket. And when you call AllocConsole later, nothing in the Console class is notified that a console now exists. It doesn't have the opportunity to attach Console.Out to the newly created standard output handle.

like image 36
Jim Mischel Avatar answered Sep 20 '22 13:09

Jim Mischel