Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Exit Collection was modified; enumeration operation may not execute

Tags:

c#

I am getting this error Collection was modified; enumeration operation may not execute.

I have 3 forms. These are the form closing events of all 3 I did some research and learnt that some from was modified/shown inturn causing this error

Form1

private void btnExitl_Click(object sender, EventArgs e)
        {
            this.Close();   
        }

private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {

     if (DataDirty)
        {
            if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))

                 Application.Exit();
            else
                e.Cancel = true;
        }
        else

             Application.Exit();
    }

Form2:

 private void btnCancel_Click(object sender, EventArgs e)
    {

        this.Close();
    }

    private void frmInputFiles_FormClosing(object sender, FormClosingEventArgs e)
    {
        int plantid = StaticClass.GlobalValue;
        //Properties.Settings.Default.PlantId = plantid;

        Program.fPlant = new frmPlant(plantid);

        Program.fPlant.Show();
        e.Cancel = false;
        //this.Hide();
    }

Form3:

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void frmVesselData_FormClosing(object sender, FormClosingEventArgs e)
    {


        DialogResult result;
        int fileId = StaticClass.FileGlobal;
        if (DataDirty)
        {
            string messageBoxText = "You have unsaved data. Do you want to save the changes and exit the form?";
            MessageBoxButtons button = MessageBoxButtons.YesNo;
            string caption = "Data Changed";
            MessageBoxIcon icon = MessageBoxIcon.Question;
            result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == DialogResult.No)
            {
                Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);

                    Program.fInputFiles.Show();
                   //e.Cancel=true;

            }
            if (result == DialogResult.Yes)
            {
                e.Cancel = true;
                //return;

            }
        }
        else
        {
            Program.fInputFiles = new frmInputFiles(gPlantId, gPlantName);
                Program.fInputFiles.Show();
                //e.Cancel = false;

        }      
    }

It happens only when I view the third form(Form3). Form1, Form2 work well,. But if I view form3 and try to go back to form1 So somewhere in the closing event of form3, form1

My guess is the btnExit_close event of the form this.close()

Thank you

like image 766
user575219 Avatar asked Jan 17 '23 06:01

user575219


2 Answers

Simply call Environment.Exit(0);

like image 136
Rohidas Kadam Avatar answered Jan 18 '23 20:01

Rohidas Kadam


While closing your First Form try this on FormClosingEvent

    private void frmPlant_FormClosing(object sender, FormClosingEventArgs e)
    {

    if (DataDirty)
    {
        if (DialogResult.Yes == MessageBox.Show("Are you sure you want to exit", "Data Changed", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
             this.Close();
             Application.Exit();
        }
        else
            e.Cancel = true;
    }
    else
         Application.Exit();
    }

Call this.Close() first and then Application.Exit(), Application.Exit() terminates all processes and Close() close your main form

like image 45
Ankush Madankar Avatar answered Jan 18 '23 20:01

Ankush Madankar