|Two minor things first:
&& (batchSize < fileList.Count)..isn't neccessary because it's covered already by this:
|| (i == (fileList.Count - 1)
if (thumbnailList.Count > 0)..isn't neccessary because if the list is empty then the foreach-loop will just do nothing.
Now, to the problem at hand - these lines of the exception stack trace:
at System.Collections.Generic.List`1.get_Item(Int32 index)
at test1.MainForm.BackgroundWorker1ProgressChanged(Object sender, ProgressChangedEventArgs e)..tell you that the problem occurs when trying to access a
List<> with its indexer in
BackgroundWorker1ProgressChanged. The only candidate for this is:
fileList[counter]So it seems somehow your
counter gets out of sync with the "actual progress". No idea why; maybe it's in the code you haven't shown here.
But that's a point I planned to suggest to you to improve anyway: Change it so that
BackgroundWorker1ProgressChanged doesn't need to assume the index of the "delivered" bitmap in
fileList by delivering not only the bitmaps but the bitmaps with their index. Either create a class for that (with Bitmap and Index as properties) and store instances of that class in the list (instead of just the Bitmap) or use a
Tuple<Bitmap, int> instead of the custom class. Or, instead of using the index, use the filename and ListViewItem.ImageKey[^] instead of
ImageIndex. That way you wouldn't need
BackgroundWorker1ProgressChanged at all.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson