0

I have a lot of forms (hidden) and i need to close them all with a button, but for some reason it started to throw errors "Collection was modified; enumeration operation may not execute"

"System.InvalidOperationException: Collection was modified; enumeration operation may not execute.  
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
   at System.Windows.Forms.Application.ExitInternal()
   at System.Windows.Forms.Application.Exit(CancelEventArgs e)
   at System.Windows.Forms.Application.Exit()
   ..."

Code

    this->button1->Click += gcnew System::EventHandler(this, &Outro::button1_Click);
...
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 Application::Exit();
             }
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
user2921071
  • 1
  • 1
  • 1
  • 4

1 Answers1

0

That's an error that's telling you that you're accessing a collection from 2 or more threads without lock protection.

Look at the CallStack and see which collection is being accessed.
That error indicates that the ArrayList was changed in a different while being iterated in that thread.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185