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

  • Home
  • SEARCH
  • 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 6942239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:59:21+00:00 2026-05-27T12:59:21+00:00

There is a sample on MSDN for .NET ThreadPool. If I run this code,

  • 0

There is a sample on MSDN for .NET ThreadPool. If I run this code, the output is completely erratic, sometimes, I get a completely empty output on the Console.

If I add a Thread.Sleep() call, even just for a few ms, the output is fine.

AFAIK Console.WriteLine() is thread-safe, so the output should always be there. But its not, at least not on my i7 2600 x64 compiled version. Obviously, if I add a breakpoint everything is fine, but it drives me mad.

I added a ConcurrentBag to make sure stuff is there, but even printing the elements of that is empty. Again, if I add a breakpoint everything is fine.

{
    public class TaskInfo
    {
        public string m_text;

            public int m_value;
        public ConcurrentBag<int> m_bag;
    public TaskInfo(string text, int value, ConcurrentBag<int> bag)
    {
        m_text = text;
        m_value = value;
        m_bag = bag;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Run();
    }

    void Run()
    {
        ConcurrentBag<int> concurrentBag = new ConcurrentBag<int>();
        for (int i = 0; i < 10; i++)
        {
            TaskInfo ti = new TaskInfo("Hello Thread", i, concurrentBag);
            bool b = ThreadPool.QueueUserWorkItem(new WaitCallback(MyThreadFunction), ti);
            if (!b)
            {
                Console.WriteLine("Damn!");
            }
            //Thread.Sleep(5);
        }

        for (int j = 0; j < concurrentBag.Count; j++)
        {
            Console.WriteLine("This is in the bag: {0}", concurrentBag.ElementAt(j));
        }
    }

    static void MyThreadFunction(object stateInfo)
    {
        TaskInfo ti = (TaskInfo)stateInfo;
        ti.m_bag.Add(ti.m_value);
        Console.WriteLine(ti.m_text + ti.m_value.ToString());
    }
}

}

  • 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-05-27T12:59:22+00:00Added an answer on May 27, 2026 at 12:59 pm

    I can very well understand that the output can be empty.

    You push 10 jobs on a queue and then immediately start consuming the results. Not all jobs will have finished, and it could be none has even started yet.

    And when running in the debugger, the program will terminate before you can see the WrtieLine()s from the MyThreadFunction.

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

Sidebar

Related Questions

There are sample codes on the net for OpenId Client but none for OpenId
In the Apple documentation for NSRunLoop there is sample code demonstrating suspending execution while
There is a sample code of apple named reachability which tells us the network
Is there any utility (or sample source code) that truncates HTML (for preview) in
Are there any sample applications avaliable that make use of both caliburn and prism?
In some sample codes there are methods and classes declared WITHIN other methods and/or
There was a fade out sample in the internet.. http://docs.dojocampus.org/dojo/fadeOut?t=tundra but i want to
there'are these lines in the sample Win32 app created default by VS. Can you
There was a library of dynamic LINQ extensions methods released as a sample with
Is there a free/open source BAR GRAPH (2d) component or sample file for Flash

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.