60

Why does Console.WriteLine work from multiple threads?

  • 14
    Why shouldn't it? – balpha Jul 03 '09 at 16:07
  • 24
    @balpha - The question is *why does it work*, *what is the reason it works*. There is nothing saying that it doesn't work. Your comment is snide and offensive. –  Jul 03 '09 at 16:18
  • 20
    I'm sorry, it wasn't meant to sound this way. What it says is: The very fact that you ask this question suggests that you think it's a suprise that it does work. I really would have liked to know why, because I know pretty mouch nothing about multi-threading and its caveats. Apology if it sounded rude. – balpha Jul 03 '09 at 16:27

4 Answers4

77

The console class handles the thread synchronization for you.

From the documentation of Console:

I/O operations using these streams are synchronized, which means multiple threads can read from, or write to, the streams.

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • @Reed is it synchronized by locking or with some other synchronization mechanism? – Kiril Apr 04 '12 at 14:45
  • 3
    @Lirik It's handled on the native side, not using a managed lock. – Reed Copsey Apr 05 '12 at 17:42
  • 4
    I would like to add that the Console window (in Visual Studio) allows writing text from multiple threads, but you should be aware that the order in which the text is output may not be as expected, e.g. when adding large strings (like stack traces) they can even end up "garbled" or mixed. There is apparently no locking that ensures one `Write` is completed before another call starts pumping text into the stream. – mike Jul 27 '18 at 09:41
  • I'm wondering, how long the text has to be to experience the garbling? I tried two similar programs, one in C++ with `cout` and another in C# with `Parallel.For` and `Console.Write`. While the C++ program did indeed give mixed output from different threads, the C# program displayed correct output and I couldn't find any way to corrupt it. – JustAMartin Sep 23 '20 at 20:10
  • @mike Do you mean that the order of calling `Console.WriteLine` from multiple threads is not the same with the order in which text appears in the console? In other words, does Console.WriteLine writes asynchronously and not in the same order it was called? – WriteEatSleepRepeat Aug 15 '22 at 18:03
  • @WriteEatSleepRepeat: The calls are posted to `Write` in the correct order and the output _starts_ in the correct order, but longer texts (e.g. stack traces) may be interrupted by shorter texts, sent later, but before the first text was fully output. At least that was my experience... – mike Aug 15 '22 at 21:31
  • @WriteEatSleepRepeat: This does *not* apply to `WriteLine`. – mike Aug 15 '22 at 21:42
11

There is a bug in .NET 4.5 CLR which makes Console.WriteLine not work from multiple threads if you use Console.ReadKey. It is fixed in some Windows versions, but in 8.1 Windows Update does not find it yet.

Infrequent hangs in a multi-threaded C# console application when using Console.Writeline() or Console.Write()

Using Console.WriteLine in a Timer why it would appear to exit?

Community
  • 1
  • 1
Jure Špik
  • 331
  • 2
  • 11
2

Multiple threads write to the same output when using Console.WriteLine, generally your screen by default.

Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
0

The console and your code are 2 seperate applications

The console window is not your application. A console application is invisable, your application does not contain the console. The console is installed into windows itself.

Console.Writeline() is a Message

It writes to a TextStream. Microsoft's console process simply waits for new streamtext to display.

The 'Console Application' name does not make it a console

The difference with ui projects is that a console application comes with a simplified set of references, and relies on input / output stream to communicate with the user.

You dont even need the console, you could build your own console, or use a debugger to view and write to it.

Comparison with notepad

If a file was a message, and notepad opened the filestream saved by your code. It wouldnt have mattered from which thread you saved the file. You are not accesing any ui.