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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:52:01+00:00 2026-05-15T21:52:01+00:00

I am attempting at embedding multi-threading for the first time ever and running into

  • 0

I am attempting at embedding multi-threading for the first time ever and running into some unexpected issues, hope you can help.

Here’s the code fragment that gives me troubles:

ArrayList recordsCollection = new ArrayList();
ArrayList batchCollection = null;
int idx = 0;

while(true)
{
  // Some code to generate and assign new batchCollection here
  recordsCollection.Add(batchCollection);

  ThreadPool.QueueUserWorkItem(delegate
  {
    ProcessCollection(recordsCollection.GetRange(idx, 1));
  });
  Interlocked.Increment(ref idx);
}

private void ProcessCollection(ArrayList collection)
{
   // Do some work on collection here
}

Once the Process Collection method is invoked and I am attempting to iterate through collection I am getting “The range in the underlying list is invalid”.

Thanks in advance!

Update: Guys, thank you to each and every one of you. Through applying your suggestions I was able to greatly simplify and get it to work.

  • 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-15T21:52:02+00:00Added an answer on May 15, 2026 at 9:52 pm

    You have a couple of problems.

    • Like Mark pointed out you are capturing a loop variable which will really confuse things here.
    • You are modifying the collection while at the same time reading it without the use of synchronization mechanisms.

    I am assuming you have omitted the code for acquiring a batchCollection and then periodically removing them from recordsCollection for brevity, otherwise there would be issues there as well.

    Here is how you can fix it.

    ArrayList recordsCollection = new ArrayList();  
    ArrayList batchCollection = null;  
    int idx = 0;  
    
    while(true)  
    {  
      lock (recordsCollection) 
      {
        recordsCollection.Add(batchCollection);  
      }
    
      int capturedIndex = idx; // Used for proper capturing.
    
      ThreadPool.QueueUserWorkItem(delegate  
      {
        ArrayList range;
        lock (recordsCollection)
        {
          range = recordsCollection.GetRange(capturedIndex, 1);
        }
        ProcessCollection(range);  
      });  
    
      idx++;
    }  
    

    Or my refactored version which, as best I can tell anyway, does the exact same thing…

    List<List<Record>> recordsCollection = new ArrayList();  
    List<Record> batchCollection = null;  
    
    while(true)  
    {  
      recordsCollection.Add(batchCollection);
    
      List<List<Record>> range = new List<List<Record>>();
      range.Add(batchCollection);
    
      ThreadPool.QueueUserWorkItem(delegate  
      {
        ProcessCollection(range);  
      });      
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • 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 The basename is structured like a fully qualified class name… May 16, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer It's true depending on the OpenGL ES version, OpenGL ES… May 16, 2026 at 9:44 pm
  • Editorial Team
    Editorial Team added an answer When working with Debian Packages files, you might find grep-dctrl… May 16, 2026 at 9:44 pm

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

Attempting to insert an escape character into a table results in a warning. For
I'm attempting to write a performance testing function that can take any function, run
I am attempting to modify the standard Swing JTree to intermingle nodes with and
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
Attempting to disable BSTR caching: SetOaNoCache(); VC++ compiler build output : 'SetOaNoCache': identifier not
Im attempting to add voteup/votedown to one of my sites, however Im having a
While attempting to find a way to merge existing PDF files and forms, I
Before attempting to resize an image in PHP using libGD, I'd like to check
I am attempting to use Reflection in C# to determine at runtime the type

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.