Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:49:06+00:00 2026-06-17T09:49:06+00:00

A colleague of mine found an issue with our code and it took a

  • 0

A colleague of mine found an issue with our code and it took a while to hunt down exactly what was happening, but it can be best demonstrated by this simple example:

// Fails
class Program
{
    static void Main(string[] args)
    {
        Task.Run(() => Console.WriteLine("Hello World"));
        Console.ReadKey();
    }
}

// Works fine
class Program
{
    static void Main(string[] args)
    {
        Console.Write(String.Empty);
        Task.Run(() => Console.WriteLine("Hello World"));
        Console.ReadKey();
    }
}

It’s clear from playing around with this that writing to the console anywhere from the main thread will allow the background thread to write to the console also, but we are struggling to understand why this is happening. Can anyone explain what writing to the console from the main thread achieves that the first snippet does not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:49:07+00:00Added an answer on June 17, 2026 at 9:49 am

    I have a suspicion as to what’s going on. What I’ve observed:

    • If you do anything with the console’s output before starting the ReadKey, it’s fine. This includes fetching Console.Out but not using it
    • If you put a delay in so that the Console.WriteLine call starts before the Console.ReadKey call, it’s fine (and you can have multiple WriteLine calls while ReadKey is waiting

    I suspect that the first operation to use the console acquires a lock for initialization (to avoid it being initialized twice) and that the ReadKey method keeps hold of the lock until a key has been read. That would certainly explain every program I’ve run so far.

    The operations which perform the hypothesized initialization are interesting though – reading Console.Out “fixes” the issue, but reading from Console.In doesn’t.

    I suspect that ReadKey initializes the output because the value is still echoed to the console… but I wouldn’t like to swear to it.

    Interestingly, using Console.ReadLine() instead of Console.ReadKey() doesn’t cause the problem in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Issue Resolved A colleague of mine was changing a cell during Workbook_BeforeSave() without
a colleague of mine wrote a webservice that runs on port 8081 of our
A colleague of mine wrote quite questionable piece of code some time ago, and
Possible Duplicate: Javascript Try-Catch Performance Vs. Error Checking Code A colleague of mine told
A colleague of mine found a way to do this a long time ago
A while ago a colleague of mine said something astonishing to me: he said
A colleague of mine wants to use my python 2 code on his OS
A colleague of mine is using a horrible source code editor that leaves strange
A colleague of mine refactored this code: private void btnGeneral_Click(object sender, RoutedEventArgs e) {
the other day a colleague of mine stated that using static classes can cause

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.