c#系统.InvalidOperationException: Collection被修改;不能执行枚举操作
本文关键字:不能 执行 枚举 操作 修改 系统 InvalidOperationException Collection | 更新日期: 2023-09-27 18:02:02
我研究了这个问题,还没有找到适合我的。我并不是在尝试编辑我正在循环的内容。
************** 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()
这是我的函数导致的:
internal static bool CloseAllForms()
{
try
{
if (clsGlobal.g_objfrmCustomerInformation != null)
clsGlobal.g_objfrmCustomerInformation.Close();
if (clsGlobal.g_objfrmSearchCustomer != null)
clsGlobal.g_objfrmSearchCustomer.Close();
if (clsGlobal.g_objfrmSwipeLicense != null)
clsGlobal.g_objfrmSwipeLicense.Close();
if (clsGlobal.g_objfrmSearchResults != null)
clsGlobal.g_objfrmSearchResults.Close();
if (clsGlobal.g_objfrmCustomerData != null)
clsGlobal.g_objfrmCustomerData.Close();
if (clsGlobal.g_objfrmPurchaseOrder != null)
clsGlobal.g_objfrmPurchaseOrder.Close();
if (clsGlobal.g_objfrmAddPurchaseOrderItem != null)
clsGlobal.g_objfrmAddPurchaseOrderItem.Close();
if ((clsGlobal.g_objfrmCustomerInformation == null) && (clsGlobal.g_objfrmSearchCustomer == null) && (clsGlobal.g_objfrmSwipeLicense == null) && (clsGlobal.g_objfrmSearchResults == null) && (clsGlobal.g_objfrmCustomerData == null) && (clsGlobal.g_objfrmPurchaseOrder == null) && (clsGlobal.g_objfrmAddPurchaseOrderItem == null))
{
PrepareImageBar();
return true;
}
else
{
PrepareImageBar();
return false;
}
}
catch (Exception ex)
{
string ErrorMessage;
ErrorMessage = "Error: " + ex.Message +
"'r'nSource: " + ex.Source +
"'r'nTargetSite: " + ex.TargetSite.ToString() +
"'r'nStackTrace: " + ex.StackTrace.ToString();
if (ex.Data.Count > 0)
{
ErrorMessage += "'r'nData Count: " + ex.Data.Count.ToString() +
"'r'nKeys: " + ex.Data.Keys.ToString() +
"'r'nValues: " + ex.Data.Values.ToString();
}
MessageBox.Show(ErrorMessage, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
CustomFunctions.emailExceptionToAuthor(ex);
return false;
}
}
我是这样调用函数的:
if (CloseAllForms())
{
if (clsGlobal.g_objfrmPurchaseOrder == null)
{
clsGlobal.g_objfrmPurchaseOrder = new frmPurchaseOrder();
clsGlobal.g_objfrmPurchaseOrder.FormClosed += PurchaseOrderFormClosed;
clsGlobal.g_objfrmPurchaseOrder.MdiParent = clsGlobal.g_objfrmMDIMain;
clsGlobal.g_objfrmPurchaseOrder.Show();
clsGlobal.g_objfrmPurchaseOrder.BringToFront();
}
else
{
clsGlobal.g_objfrmPurchaseOrder.Show();
clsGlobal.g_objfrmPurchaseOrder.BringToFront();
}
PrepareImageBar();
}
请帮帮我!
更新:这里是完整的代码。
例外有关调用的详细信息,请参阅此消息的末尾即时(JIT)调试,而不是这个对话框。
************** 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()
at Microsoft.VisualBasic.PowerPacks.ShapeCollection.Dispose(Boolean disposing)
at Microsoft.VisualBasic.PowerPacks.ShapeContainer.Dispose(Boolean disposing)
at System.ComponentModel.Component.Dispose()
at System.Windows.Forms.Control.Dispose(Boolean disposing)
at System.Windows.Forms.Form.Dispose(Boolean disposing)
at B2HD_Software.frmCustomerData.Dispose(Boolean disposing)
at System.Windows.Forms.Form.WmClose(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
PrepareImageBar ();
internal static void PrepareImageBar()
{
clsGlobal.g_objfrmMDIMain.mnuImageBarAddCustomer.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarAddCustomer.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarEditCustomer.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarEditCustomer.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarFindCustomer.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarFindCustomer.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarStartSearch.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarStartSearch.Enabled = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarSwipeLicense.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarSwipeLicense.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarViewNotes.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarViewNotes.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarNewPurchase.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarNewPurchase.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarAddItem.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarAddItem.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarModifyItem.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarModifyItem.Enabled = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarDeleteItem.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarDeleteItem.Enabled = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarTakePicture.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarTakePicture.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSavePicture.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarSavePicture.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSave.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarSave.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Enabled = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarAdmin.Visible = false;
clsGlobal.g_objfrmMDIMain.mnuImageBarAdmin.Enabled = true;
switch (clsGlobal.ActiveForm())
{
case "CustomerInformation":
clsGlobal.g_objfrmMDIMain.mnuImageBarTakePicture.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSave.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Enabled = true;
break;
case "SearchCustomer":
clsGlobal.g_objfrmMDIMain.mnuImageBarStartSearch.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSwipeLicense.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarStartSearch.Enabled = false;
break;
case "SwipeLicense":
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
break;
case "CustomerData":
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarViewNotes.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarNewPurchase.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarEditCustomer.Visible = true;
break;
case "PurchaseOrder":
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarAddItem.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarDeleteItem.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarModifyItem.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarEditCustomer.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarViewNotes.Visible = true;
clsGlobal.g_objfrmPurchaseOrder.ShowProperButtons();
break;
case "AddPurchaseOrderItem":
clsGlobal.g_objfrmMDIMain.mnuImageBarTakePicture.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSave.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarCancel.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarViewNotes.Visible = true;
break;
default:
clsGlobal.g_objfrmMDIMain.mnuImageBarAddCustomer.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarFindCustomer.Visible = true;
clsGlobal.g_objfrmMDIMain.mnuImageBarSwipeLicense.Visible = true;
break;
}
}
CloseAllForms ()
internal static bool CloseAllForms()
{
if (clsGlobal.g_objfrmCustomerInformation != null)
clsGlobal.g_objfrmCustomerInformation.Close();
if (clsGlobal.g_objfrmSearchCustomer != null)
clsGlobal.g_objfrmSearchCustomer.Close();
if (clsGlobal.g_objfrmSwipeLicense != null)
clsGlobal.g_objfrmSwipeLicense.Close();
if (clsGlobal.g_objfrmSearchResults != null)
clsGlobal.g_objfrmSearchResults.Close();
if (clsGlobal.g_objfrmCustomerData != null)
clsGlobal.g_objfrmCustomerData.Close();
if (clsGlobal.g_objfrmPurchaseOrder != null)
clsGlobal.g_objfrmPurchaseOrder.Close();
if (clsGlobal.g_objfrmAddPurchaseOrderItem != null)
clsGlobal.g_objfrmAddPurchaseOrderItem.Close();
if ((clsGlobal.g_objfrmCustomerInformation == null) && (clsGlobal.g_objfrmSearchCustomer == null) && (clsGlobal.g_objfrmSwipeLicense == null) && (clsGlobal.g_objfrmSearchResults == null) && (clsGlobal.g_objfrmCustomerData == null) && (clsGlobal.g_objfrmPurchaseOrder == null) && (clsGlobal.g_objfrmAddPurchaseOrderItem == null))
{
PrepareImageBar();
return true;
}
else
{
PrepareImageBar();
return false;
}
}
这是我的关闭事件在每个表单上的样子。
internal static void CustomerInformationFormClosed(object sender, FormClosedEventArgs e)
{
clsGlobal.g_objfrmCustomerInformation = null;
PrepareImageBar();
}
我认为有三种可能性:
- 某些东西在
FormClosed
事件处理程序中失败的形式之一(即,可能是PurchaseOrderFormClosed
中的某些东西)。 - 您的
CloseAllForms()
方法正在catch
中做一堆工作。有可能代码在那里失败了。当错误发生时,您看到MessageBox
了吗?邮件发送了吗?试着注释掉所有的代码,看看是否仍然会出现错误。或者暂时用MessageBox.Show(ex.ToString())
替换catch
中的所有代码,这样你就可以确保你看到的异常发生在try
中。 -
PrepareImageBar()
出现故障。试着注释掉这个方法,看看你是否仍然得到错误。
我不知道异常发生在哪里,但异常可能在foreach
代码块中生成,其中修改了集合。这里,修改的意思是"添加"或"删除",比如:
foreach(var item in collection) { collection.Add(other);}
枚举时不能修改集合。
要找到它发生的地方,如果是在你的代码,而调试,你会检查'用户未处理的选项'在例外对话框(Ctrl+Alt+E)。但是,请注意,要使用此选项,VS必须在调试选项中设置为"只启用我的代码"。
我见过这个bug:这个错误显然只出现在一些windows安装上,我从来没有通过调试让它发生在开发环境中,只有完整的应用程序。
这是VisualBasic.PowerPacks中的一个错误。
请参阅此处查看解决方案(请参阅博客末尾)。
您基本上必须重写窗体的Dispose()方法并手动处置所有VisualBasic Powerpack项。