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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:39:01+00:00 2026-06-05T06:39:01+00:00

public void GatherDataFromSwitches(Device[] switches) { List<Thread> workerThreads = new List<Thread>(); for(int i = 0;

  • 0
public void GatherDataFromSwitches(Device[] switches)
{
    List<Thread> workerThreads = new List<Thread>();
    for(int i = 0; i < switches.Length - 1; i++)
    {
        Thread t = new Thread(unused => GatherDataFromSwitch(switches[i]));
        workerThreads.Add(t);
        t.Start();
    }
    foreach (Thread d in workerThreads) d.Join(); //wait for all threads to finish
}

If I loop through switches after having run that method I notice that somehow some switches had no added data, and some switches had data added from multiple switches. So something went wrong with passing the reference to the worker threads. I’m still not sure what exactly, but I solved the problem by adding

Thread.Sleep(100); 

right after

t.Start();

I’m assuming this works because now the newly created thread has some time to initialize before the next is created. But this is a work around, not a fix. Is it because of how lambda expressions work?

How do I properly go around this?

  • 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-05T06:39:02+00:00Added an answer on June 5, 2026 at 6:39 am

    The problem is the way i is captured in the lambda. Make a local copy inside the loop to have each lambda capture a distinct value:

    public void GatherDataFromSwitches(Device[] switches)
    {      
        List<Thread> workerThreads = new List<Thread>();
        for(int i = 0; i < switches.Length ; i++)
        {
            int j = i; // local i
            Thread t = new Thread(unused => GatherDataFromSwitch(switches[j]));
            workerThreads.Add(t);
            t.Start();
        }
        foreach (Thread d in workerThreads) d.Join(); //wait for all threads to finish
    }
    

    Or pass i explicitly as a parameter to the thread:

    public void GatherDataFromSwitches(Device[] switches)
    {      
        List<Thread> workerThreads = new List<Thread>();
        for(int i = 0; i < switches.Length ; i++)
        {
            Thread t = new Thread(param => { j = (int)param; GatherDataFromSwitch(switches[j]); });
            workerThreads.Add(t);
            t.Start(i);
        }
        foreach (Thread d in workerThreads) d.Join(); //wait for all threads to finish
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int
public void Foo(IRB inR) { Stopwatch sw = new Stopwatch(); sw.Start(); System.Threading.Thread theThread =
public void sort() { datelist = new List<DateTime>(rdate); //timedates intlist = new List <Int>(rint);
public void DoRiskyThings(List<Action> tasks) { List<Exception> exceptions = new List<Exception>(); foreach(Action task in tasks)
public void Draw(SpriteBatch theSpriteBatch) { Random rand = new Random(); for(int y = 0;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadData(); setContentView(R.layout.preload); Thread thread = new Thread() {
public void AddProfile() { //Add conventions for DX Components. Profile newProfile = new Profile()
public void wahey(List<Object> list) {} wahey(new LinkedList<Number>()); The call to the method will not
public void drawboard(Graphics g){ g.setColor(Color.yellow); for (int row = 0; row < board.length; row++){
public void Foo (IEnumerable<object> objects) { } var strings = new List<string>{first, second, third};

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.