2

I have researched this issue and haven't found what pertains to me yet. I am not trying to edit something I am looping through.

************** 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()

Here is my function that is causing it:

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;
    }
}

This is how I call the function:

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();
}

Please help me!

UPDATE: Here is the full code.

The exception See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

************** 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;
}

}

This is what my close event looks like on each form.

internal static void CustomerInformationFormClosed(object sender, FormClosedEventArgs e)
    {
        clsGlobal.g_objfrmCustomerInformation = null;

        PrepareImageBar();
    }
Brandon
  • 1,735
  • 2
  • 22
  • 37
  • 2
    Can you provide a full stack trace? Do you know where the code fails (maybe something in `PrepareImageBar` perhaps? – rsbarro Jul 14 '11 at 18:00
  • what platform? (asp? silverlight? wpf? console? etc.) – Muad'Dib Jul 14 '11 at 18:01
  • 4
    There is some code missing here; probably one that has a `foreach` loop over a List, and somewhere in the `foreach` loop you are attempting to add or remove an item in the collection. – vcsjones Jul 14 '11 at 18:01
  • +1 on @vcsjones comment, most likely you are modifying a collection while looping through it either on the same or a different thread. – James Michael Hare Jul 14 '11 at 18:07
  • Brandon, none of the code you supplied is relevant to the error message you're getting. – Mike Atlas Jul 14 '11 at 18:28
  • 1
    Remove the try and catch, it is preventing you from seeing where the code really bombs. Leave it out. Use AppDomain.UnhandledException instead. – Hans Passant Jul 14 '11 at 19:52
  • I know that is the thing that makes me crazy. PrepareImageBar() just shows/hides some buttons... If I comment the "if (CloseAllForms())" portion, no error! – Brandon Jul 14 '11 at 19:55
  • I tried the AppDomain.UnhandledException thing it still does the same thing – Brandon Jul 14 '11 at 22:31

3 Answers3

1

I don't know where the exception was occurred but the exception might be generated in foreach code block where the collection was modified. Here, the modification means 'Add' or 'Remove' something like:

foreach(var item in collection)    { collection.Add(other);}

A collection cannot be modified while enumerating.


UPDATED

To find where it was occurred if being in your code, while debugging, you would check the 'user unhandled option' in Exceptions dialog (Ctrl+Alt+E). However, please note that to use this option, VS has to be set 'Enable just My Code' in Debugging option.

Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45
  • I have narrowed it down to the function I posted. If I do not run the if (CloseAllForms()) portion of my code in my event the exception is not thrown. I have tried EVERY possible thing I can do to catch the exception and deal with it and it still throws an unhandled exception. Also the options you said to enable are already enabled. I couldn't find the "Just my code" option but debug unmanaged code is disabled I think this is what you wanted? I cannot figure this out because I am not changing a collection as far as I can tell. – Brandon Jul 14 '11 at 19:38
  • I see. The testing with only your posted code would be difficult. Where VS stopped in your source code with this exception? – Jin-Wook Chung Jul 14 '11 at 19:46
  • That's the thing (I forgot to mention it though). When i test it on a computer that has VS installed, debugging or not...no error. When I deploy it on another workstation, it error's. Doesn't give me a line of code or anything. I just know that after trial and error if I don't run the CloseAllForms() it doesn't error. Is there a better way to make sure all MDI children are closed that I could try? – Brandon Jul 14 '11 at 19:50
  • I see that this is exact your problem. I guess it would result from the child disposing, but don't know exactly yet. If finding goodness, I'll show you it... – Jin-Wook Chung Jul 14 '11 at 20:01
1

I see three possibilities:

  1. Something is failing in the FormClosed event handler for one of the forms (i.e., maybe something in PurchaseOrderFormClosed).
  2. Your CloseAllForms() method is doing a bunch of work in the catch. It could be possible that the code is failing there. When the error occurs, do you see the MessageBox? Does the email get sent? Try commenting out all of the code there and see if you still get the error. Or maybe just temporarily replace all of the code in the catch with a MessageBox.Show(ex.ToString()) so you can be sure the exception you are seeing is occurring in the try.
  3. Something is failing in PrepareImageBar(). Try commenting out that method and see if you still get the error.
rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • I actually only put all that stuff in the catch because I was trying to come up with a way to make it give me anything useful. I do not get a messagebox. I just updated my question with full code of all the methods that could be failing. – Brandon Jul 14 '11 at 22:22
  • From your stack trace it looks like it's failing when trying to close `frmCustomerData`. Is there anything going on in the `Close` or `Dispose` methods for that form? If you're not getting the message box I would suspect that the exception is occurring on a separate thread, so maybe wire up a handler for the AppDomain.UnhandledException event when your application start up (as suggested in the comments to your question). – rsbarro Jul 14 '11 at 22:34
  • I updated my code above to show the Closed event. I don't know how to access one form from another without having a global variable set to each form...so when the form is closed it sets the variable to null....Is there a better way to do it? – Brandon Jul 14 '11 at 22:37
  • 1
    If you skip closing frmCustomerData, do you still get the error? Maybe it has to do with the PowerPacks. – rsbarro Jul 14 '11 at 22:52
  • 1
    Thank you!!!!!! It was a few stupid lines I drew using the VB powerpacks. If I get rid of them the error goes away. Must be a problem disposing them. I will just draw them manually. Thank you for fixing this 4 day headache! – Brandon Jul 15 '11 at 00:04
0

I've seen this bug: The error apparently only shows up on some windows installs, and I've never gotten it to occur in the development environment via debug, only the full app.

This is a bug in the VisualBasic.PowerPacks.

See here for a solution (see the end of the blog).

You basically have to override the forms Dispose() method and manually dispose of all VisualBasic Powerpack items.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291