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 1106693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:49:52+00:00 2026-05-17T01:49:52+00:00

I am testing a simple threadpool solution before I put it in my application,

  • 0

I am testing a simple threadpool solution before I put it in my application, but the results I am seeing do not make sense for me. I have a simple form with one button on it. This button starts the following loop:

private void button1_Click(object sender, EventArgs e)
{
    MyTestThread oTask = new MyTestThread ();
    MyThreadInfo oTaskParameters = new MyThreadInfo();
    for (int i = 1; i <= 5; i++)
    {
        objTaskParameters.MyGuid = Guid.NewGuid();
        objTaskParameters.MyNumber = i;
        ThreadPool.QueueUserWorkItem(new WaitCallback(objTask.ProcessDataForNTime), objTaskParameters);
    }
    Console.WriteLine("All threads have been queued for processing...");
}

The class it is calling, looks like this. It has a parameters MyThreadInfo class, and then the MyTestThread class just loops for 10 seconds before finishing.

public class MyThreadInfo
{
    public int MyNumber;
}

public class MyTestThread 
{
    public void ProcessDataForNTime(Object oParameters)
    {
        //We pass parameters
        MyThreadInfo oThread = (MyThreadInfo)oParameters;
        int threadNo = oThread.MyNumber;

        Console.WriteLine("thread {0} started...", threadNo);

        int iN = 10; //Delay for 10 seconds
        DateTime dteStart = DateTime.Now;
        do
        {
            System.Threading.Thread.Sleep(1000); //Wait a second before we look again
        } while (dteStart.AddSeconds(iN) > DateTime.Now);

        Console.WriteLine("thread {0} completed...", threadNo);
    }
}

I would expect the results of this to display something like this in my console/output/debug log. When I step through the application, I do in fact see this:

thread 1 started...
thread 2 started...
thread 3 started...
thread 4 started...
thread 5 started...
All threads have been queued for processing...
thread 1 completed...
thread 2 completed...
thread 3 completed...
thread 4 completed...
thread 5 completed...

BUT, if I run the application at full speed, without stepping through the code or breakpoints, it outputs this:

All threads have been queued for processing...
thread 5 started...
thread 5 started...
thread 5 started...
thread 5 started...
thread 5 started...
thread 5 completed...
thread 5 completed...
thread 5 completed...
thread 5 completed...
thread 5 completed...

What is it about stepping through the code that makes it work? I did note that adding a Thread.Sleep(1000) (1 second) to the loop does force the application to label the threads correctly, but I’m trying to develop a fast multithreading application, and the idea of adding a 1 second delay for each thread is frustrating.

Can anybody give me some insight about why I am seeing this behaviour?

thanks…

Sam

  • 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-17T01:49:52+00:00Added an answer on May 17, 2026 at 1:49 am

    You need to pass each new thread an entirely new object. Currently they all get a reference to the same object in the main thread, i.e. oTaskParameters

    Try this, after making MyNumber settable as a param on MyThreadInfo constructor:

    for (int i = 1; i <= 5; i++)
    {
        MyThreadInfo oTaskParameters = new MyThreadInfo(i);
        objTaskParameters.MyGuid = Guid.NewGuid();
        ThreadPool.QueueUserWorkItem(new WaitCallback(objTask.ProcessDataForNTime), objTaskParameters);
    }
    
    MyThreadInfo(int i) : MyNumber(i)
    {
      // rest of construction logic
    }
    

    This change means that each new thread gets a disjoint instance of MyThreadInfo, with the id for that thread set appropriately.

    There is still no guarantee that the threads will print out their ids in ascending order. You’d have to introduce some type of FIFO queue/processing to enforce that. There is a detailed discussion of that issue ordering of thread execution using Threadpool.QueueUserWorkItem.

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

Sidebar

Ask A Question

Stats

  • Questions 541k
  • Answers 541k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's easy - Just Follow the instructions - Calling Python… May 17, 2026 at 2:52 am
  • Editorial Team
    Editorial Team added an answer Ok. I figured it out , anonymous user does't have… May 17, 2026 at 2:52 am
  • Editorial Team
    Editorial Team added an answer With def duration=(d) self.duration = "#{d[:hours]}:#{d[:minutes]}:00" end In your method… May 17, 2026 at 2:52 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

When I'm testing my simple geometric library, I usually have one or two methods
I am testing a simple multicast application on linux. I got it working on
It appears to from my simple testing but I'm wondering if this is guaranteed?
I have the following JUnit test. The method that I'm testing is quite simple,
I'm creating a simple blackberry application for testing purposes and my custom buttons do
I have a very simple AsyncTask implementation example and am having problem in testing
I'm trying to write a simple bash script but something seems wrong, I'm testing
I am testing a simple query to get data from an AS400 database. I
I'm testing my simple OpenGL ES implementation (a 2D game) on the iPhone and
I'm just testing a simple databind expression with: <div> Now: <%# DateTime.Now.ToString()%> </div> According

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.