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.
You have a couple of problems.
I am assuming you have omitted the code for acquiring a
batchCollectionand then periodically removing them fromrecordsCollectionfor brevity, otherwise there would be issues there as well.Here is how you can fix it.
Or my refactored version which, as best I can tell anyway, does the exact same thing…