我有多线程应用程序,我得到这个错误
************** 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()
...
Run Code Online (Sandbox Code Playgroud)
我的收藏可能有问题,因为在一个线程上我读取了我的收藏,在另一个线程上我修改了收藏.
public readonly ObservableCollectionThreadSafe<GMapMarker> Markers = new ObservableCollectionThreadSafe<GMapMarker>();
public void problem()
{
foreach (GMapMarker m in Markers)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我试图用这段代码锁定集合,但不起作用.
public void problem()
{
lock(Markers)
{
foreach (GMapMarker m in Markers)
{
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么想法来解决这个问题?
我有一个datagridview有五列和上下文菜单条,其中包含项目和子项目.当我右键单击最后一列时,我想打开上下文菜单.
我尝试了这段代码,但它是没有子项的打开上下文菜单条.
dataGrid.Columns[dataGrid.Columns.Count].HeaderCell.ContextMenuStrip = contextMenuStrip1;
Run Code Online (Sandbox Code Playgroud)