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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:29:11+00:00 2026-06-12T19:29:11+00:00

I have a method that accepts a List<int> called DoWork . I have a

  • 0

I have a method that accepts a List<int> called DoWork. I have a huge List<int> Ids. I split the huge list into 4 sub lists:

List<List<int>> t = (List<List<int>>)SplitColumn<int>(Ids);

(SplitColumn is slightly modified from the answer to splitting a list into sub lists).

I paused the program and inspected t with the debugger and it is four lists divided exactly as I would expect.

Then, what I’m trying to do is spawn four threads (one for each sublist). The part I’m having trouble with is passing the four lists. I am getting out of bounds problems, I’m not sure what’s going on here:

        List<Thread> threads = new List<Thread>();

        for(int i = 0; i < t.Count; i++) 
        {
            threads.Add(new Thread(() => DoWork(t[i])));
        }

        foreach (Thread thread in threads)
        {
            thread.Start();
        }


        foreach (Thread thread in threads)
        {
            thread.Join();
        }
  • 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-12T19:29:12+00:00Added an answer on June 12, 2026 at 7:29 pm

    This is a classic, called capturing the loop variable.

    In this code, the same variable i is shared by all threads. By the time the threads are running the main thread will have made i == t.Count, hence the range exception.

        for(int i = 0; i < t.Count; i++) 
        {
            threads.Add(new Thread(() => DoWork(t[i])));
        }
    

    To fix it:

        for(int i = 0; i < t.Count; i++) 
        {
            int copy = i;
            threads.Add(new Thread(() => DoWork(t[copy])));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that accepts a sender and is called with a UIbutton.
I have a WCF service method that accepts a List of objects. For this
I have a method that accepts a generic delegate as a parameter, and inserts
I have a method that accepts an IEnumerable-decimals and performance various math functions. I
I have a method Foo4 that accepts a parameter of the type Func<>. If
I have a method, serializetoxml , that accepts an object of type obj1 as
I have a method that accepts an Expression<Func<T, bool>> as a parameter. I would
I would like to have a method in an interface that accepts any Type
Like java, I want to create a method that accepts an array list of
I have method that returns Drawable , and if its Bitmap object is recycled

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.