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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:51:59+00:00 2026-06-07T19:51:59+00:00

I have a main thread that makes some other threads in two nested for.

  • 0

I have a main thread that makes some other threads in two nested for.

private void mainthread()
        {
            List<Thread> ts= new List<Thread>();

            for (int w=0; w<7; w+=2)
                for (int h = 0; h < 5; h+=3)
                {
                    Thread t = new Thread(delegate() { otherthreads(w, h); });
                    ts.Add(t);
                    t.Start();
                }
            for (int i = 0; i < ts.Count; i++)
                ts[i].Join();
        }

        private void otherthreads(int w, int h)
        {                    
            listBox1.Invoke(new singleparam(addtolistbox), new object[] { "w:" + w.ToString() + ",h:" + h.ToString() });        
        }

Each thread adds it’s input arguments to a Listbox. I am confused why the input arguments of some threads are not in the for bounds?

enter image description here

  • 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-07T19:52:01+00:00Added an answer on June 7, 2026 at 7:52 pm

    Your loop is running correctly, but what is happening is this: the delegate knows that it must pass w and h onto the otherthreads() function, but those values are not bound until it is actually invoked. In other words, prior to the delegate actually executing, it just knows it must use w and h. On your last iteration, you are asking for the delegate to execute, but before it can, w and h increment for the final time on the initiating thread, causing their values to be 8 and 6, respectively. The loops exit. Then, picomoments later, the delegate executes and NOW has the values of w and h… but the values are now 8 and 6.

    You can avoid this by “snapshotting” w and h with local variables to the tightest scope around the delegate and assigning their values appropriately:

    for (int h = 0; h < 5; h+=3) 
    {
        int h2=h;
        int w2=w;
    
        Thread t = new Thread(delegate() { otherthreads(w2, h2); });
        ts.Add(t);
        t.Start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
I have a program that runs in a few threads. The main thread shares
In my sample application, I have basically two threads. The main thread contains a
I have two classes - one that runs in the main thread and performs
I have one main thread that does some rather CPU intensive operation. The thread
I have a main thread that invokes a child thread function at different times
I have a windows form on the main thread and another thread that does
I have code that reloads images via HTTP from the main thread and displays
I have a local thread that is started on button-click event from the main
I have a class class with main thread public class MainThread extends Thread {

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.