【发布时间】:2011-03-19 18:14:39
【问题描述】:
我第一次尝试嵌入多线程,遇到了一些意想不到的问题,希望你能帮忙。
这是给我带来麻烦的代码片段:
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
}
一旦调用 Process Collection 方法并尝试遍历集合,我就会得到“基础列表中的范围无效”。
提前致谢!
更新:伙计们,感谢你们每一个人。通过应用您的建议,我能够大大简化并使其发挥作用。
【问题讨论】:
-
它不能解决您的问题,但您可能需要考虑使用
List<T>而不是ArrayList。 -
谢谢马克,我肯定会这样做,难怪我没有找到 ArrayList 的泛型版本。 :)
标签: c# .net multithreading collections