我需要幫助。我正在與一個數據列表工作,並突然發現這個錯誤。收藏已修改;枚舉操作可能不會執行。 C#
'System.InvalidOperationException' 類型的未處理的異常出現在mscorlib.dll
其他信息:集合已修改;枚舉操作可能不會執行。
這是它顯示了異常的代碼......
foreach (PC_list x in onlinelist) {
if ((nowtime.Subtract(x.time)).TotalSeconds > 5) {
Invoke(new MethodInvoker(delegate {
index = Main_ListBox.FindString(x.PcName);
if(index != ListBox.NoMatches)
Main_ListBox.Items.RemoveAt(index);
}));
onlinelist.Remove(x);
//Thread.Sleep(500);
}
}
凡
注:
- onlinelist是一個ArrayList
- nowtime和x.time是DateTime。
調用堆棧
mscorlib.dll!System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext() + 0x122 bytes
BlueBall.exe!BlueBall.BlueBall.clean_arraylist() Line 74 + 0x1a8 bytes C#
BlueBall.exe!BlueBall.BlueBall.server() Line 61 + 0x8 bytes C#
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) + 0x63 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool ignoreSyncCtx) + 0xb0 bytes
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x2c bytes
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x44 bytes
[Native to Managed Transition]
這不是這麼回事。在foreach循環中,不能從列表中刪除任何項目。 – phoog