首页 新闻 会员 周边

求助:System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。

0
悬赏园豆:10 [待解决问题]

2013-05-27 09:17:55,722 [1] ERROR Leadsoft.SuperDataCenter.Program -System.InvalidOperationException: 集合已修改;可能无法执行枚举操作。
   在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   在 System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   在 System.Collections.Generic.List`1.Enumerator.MoveNext()
   在 DevComponents.DotNetBar.SuperGrid.GridPanel.MeasureRealRows(GridLayoutInfo layoutInfo, GridLayoutStateInfo stateInfo, Size constraintSize, Size& sizeNeeded)
   在 DevComponents.DotNetBar.SuperGrid.GridPanel.MeasureRows(GridLayoutInfo layoutInfo, GridLayoutStateInfo stateInfo, Size constraintSize, Size& sizeNeeded)
   在 DevComponents.DotNetBar.SuperGrid.GridPanel.MeasureOverride(GridLayoutInfo layoutInfo, GridLayoutStateInfo stateInfo, Size constraintSize)
   在 DevComponents.DotNetBar.SuperGrid.GridElement.Measure(GridLayoutInfo layoutInfo, GridLayoutStateInfo stateInfo, Size constraintSize)
   在 DevComponents.DotNetBar.SuperGrid.SuperGridControl.ArrangeGrid(Graphics g)
   在 DevComponents.DotNetBar.SuperGrid.SuperGridControl.OnPaint(PaintEventArgs e)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 DevComponents.DotNetBar.SuperGrid.SuperGridControl.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

读取列表时偶尔会发生改错误,是什么原因呢?读取时没有用到foreach,但用到BackgroundWorker多线程控件。

问题补充:

发生这个错误的几率不大,有可能一千次才发生一次,没有用foreach修改集合

出现现象: superGridControl控件显示大红叉

chenhaixia的主页 chenhaixia | 初学一级 | 园豆:174
提问于:2013-05-27 10:22
< >
分享
所有回答(4)
1

foreach操作的对象,不能在里面对该集合进行Add,Remove等,修改集合的操作。

可以选择使用for循环。

Supper_litt | 园豆:827 (小虾三级) | 2013-05-27 10:37
0

多线程操作集合了.

可以在操作的时候加同步锁.

只会造轮子 | 园豆:2274 (老鸟四级) | 2013-05-27 11:57

好 我也遇见了这种问题但是很代码结构是这样的这样我也跑了个案例没见到多线程操作是否需要加同步锁

Dictionary<int, int> dctlist = new Dictionary<int, int>(); foreach (System.Diagnostics.ProcessThread thread in System.Diagnostics.Process.GetCurrentProcess().Threads) { dctlist[thread.Id] = 0; } List<int> removeList = new List<int>(); foreach (int id in Props.Keys) { if (!dctlist.ContainsKey(id)) { removeList.Add(id); } } for (int i = 0; i < removeList.Count; i++) { Props.Remove(removeList[i]); } removeList.Clear();

支持(0) 反对(0) IT_小子 | 园豆:17 (初学一级) | 2017-10-20 08:59

十分急能否给点资料

支持(0) 反对(0) IT_小子 | 园豆:17 (初学一级) | 2017-10-20 09:01
0

用for 循环就好了。

酒 Leo | 园豆:202 (菜鸟二级) | 2013-05-27 15:02
0

是在同一时间对两个集合进行了循环操作且对集合内的元素进行了改变(下标移位)

长沙大鹏 | 园豆:214 (菜鸟二级) | 2019-01-19 21:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册