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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:01:32+00:00 2026-05-26T16:01:32+00:00

I want to run a class with different value in a thread list. like

  • 0

I want to run a class with different value in a thread list. like this :

     int index = 0;
     foreach (TreeNode nd in tvew.Nodes[0].Nodes)
     {
         threadping[index] = new Thread(delegate()
         { this.Invoke(new DelegateClientState(InvokeCheckNetworkState), new object[] {nd}); });

         threadping[index].Name = nd.Name;
         threadping[index].IsBackground = true;
         threadping[index].Start();

         index++;
     }

but when i debug the code, i see that the class parameter is just the last value.
i mean when i go through thread class i see that each time that class run the value of the input parameter is the last value for the last thread.

Can anybody tell me WHY?

  • 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-26T16:01:32+00:00Added an answer on May 26, 2026 at 4:01 pm

    It’s because the nd variable is captured in a closure. When the threads run, they are all referencing the same TreeNode instance, namely the last one that was assigned to nd. To fix, use a separate variable that doesn’t change within the scope:

     foreach (TreeNode nd in tvew.Nodes[0].Nodes)
     {
         var current = nd;
         threadping[index] = new Thread(delegate()
         { this.Invoke(new DelegateClientState(InvokeCheckNetworkState), new object[] {current}); });
    

    If we get compiler-technical, this happens because the compiler generates an anonymous class containing your loop variable in order to make it accessible for the thread delegate. This is expected behaviour, although maybe a bit counter-intuitive when you run into it the first time.

    For a longer explanation of closures and the capture of variables, see the Captured Variables section here (bottom section) in a Jon Skeet article, or this article from Eric Lippert. This is commonly known as an “access to modified closure” bug. If you search for the term on StackOverflow or Google, you will get a lot of hits explaining it.

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

Sidebar

Related Questions

I want to discover at run-time ONLY the static Methods of a class, how
I want to write a simple class (PHP5) that can 'run' an unknown amount
Why this code don't work,when i want run this code vwd 2008 express show
I have a sub-procedure which I want to run a different process, depending on
I have a custom control like this: public class CustomControl1 : Control { private
I want run a script as follows: runner: ssh 'java program &' ssh 'java
I want to run a weekly batch process in an asp.net page. How can
I want to run a web application on php and mysql, using the CakePHP
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I want to run a command as soon as a certain text appears in

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.