none
close many instance throws :collection was modified enumeration operation may not execute c#. winform

    Question

  • Hi;

    I have an application with 2 form: mainForm and second form owned by mainForm "cameraViewVS", so i create many instance for the second to show many popUp.

    now i want when i click on reload button, all the popUp will be closed.

    I used this:

    private void btnReloadCamMarker_Click(object sender, EventArgs e)
            {            
                if (checkBoxAlarm.Checked == true)
                {                
                    checkBoxAlarm.Checked = false;
                    foreach (Form S in Application.OpenForms)
                    {
                        var frm = S as Formes.CameraViewVS;
                        if (frm != null)
                            frm.Close();
                    }                               
                }
            }

    when i click, i have a throw "Collection was modified; enumeration operation may not execute."

    Any idea?

    Friday, May 18, 2012 8:44 AM

Answers

  • You're iterating through Application.OpenForms and closing each one; that modifies the OpenForms collection and .Net is getting quite upset that it's losing it's place.

    Transfer the forms you want to close to a different list, then close each of those, as in :

    // Create a List to hold the Forms to be closed 
    List<Form> targets = new List<Form>(); 
    
    // Find each form to close ... 
    foreach( Form frm in Application.OpenForms )
       if ( frm is Formes.CameraViewVS ) 
          targets.Add( frm ); 
    
    // ... and then close each in turn. 
    foreach( Form frm in targets ) 
       frm.Close(); 
    


    Regards, Phill W.

    • Proposed as answer by Kris444 Friday, May 18, 2012 6:20 PM
    • Marked as answer by Ostorlabi Tuesday, May 22, 2012 9:33 AM
    Friday, May 18, 2012 1:00 PM

All replies

  • You're iterating through Application.OpenForms and closing each one; that modifies the OpenForms collection and .Net is getting quite upset that it's losing it's place.

    Transfer the forms you want to close to a different list, then close each of those, as in :

    // Create a List to hold the Forms to be closed 
    List<Form> targets = new List<Form>(); 
    
    // Find each form to close ... 
    foreach( Form frm in Application.OpenForms )
       if ( frm is Formes.CameraViewVS ) 
          targets.Add( frm ); 
    
    // ... and then close each in turn. 
    foreach( Form frm in targets ) 
       frm.Close(); 
    


    Regards, Phill W.

    • Proposed as answer by Kris444 Friday, May 18, 2012 6:20 PM
    • Marked as answer by Ostorlabi Tuesday, May 22, 2012 9:33 AM
    Friday, May 18, 2012 1:00 PM
  • Thank you Phill.
    Friday, May 18, 2012 1:22 PM
  • Hi Fill;

    Now I remove from the same list the instance which is closed.

    And when i want to close all instance i have the same error;

    here the event where i remove the instance closed

    public void ClosePopup(object sender, System.EventArgs e)
            {
                Form currentForm = sender as Form;
                Formes.CameraViewVS frm = currentForm as Formes.CameraViewVS;
                camPopup.Remove(frm);           
            }

    and here i want to close all:

    private void btnReloadCamMarker_Click(object sender, EventArgs e)
            {                          
                
    
                foreach (Form S in camPopup)
                {
                    S.Close();
                }

    How could i fix it?

    Tuesday, May 22, 2012 9:38 AM
  • Hi Ostorlabi,

    Add the closed form to a new list and when you finish close all the forms, iterate through the new list and remove them from the camPopup list.

    If there is anything unclear, please let us know.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 23, 2012 1:29 PM
    Moderator