我多线程应用程序,我得到这个错误集合被修改,枚举操作可能不会执行
************** Exception Text **************
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
...
我可能有我收集的问题,因为在一个线程我读我的收集和在另一个线程我修改采集。
public readonly ObservableCollectionThreadSafe<GMapMarker> Markers = new ObservableCollectionThreadSafe<GMapMarker>();
public void problem()
{
foreach (GMapMarker m in Markers)
{
...
}
}
我想用此代码锁定集合,但不起作用。
public void problem()
{
lock(Markers)
{
foreach (GMapMarker m in Markers)
{
...
}
}
}
任何想法解决这个问题?
您的问题是用'foreach'里面的代码,请张贴。 – nemesv 2012-03-29 12:16:42
你无法修改集合,而循环与foreach – Reniuz 2012-03-29 12:16:46