why it is not throwing "Collection was modified; enumeration operation may not execute" exception

781 views Asked by At

I have a logout menu option in my MDI application. On log out I want to close all open forms. currently i am using following code snippet to achieve this;

For Each f As Form In My.Application.OpenForms
    If f.Name = Me.Name Then
        For Each child As Form In f.MdiChildren
            child.Close()
        Next
    Else
        f.Close()
    End If
Next

It is working perfect in my test environment, even though I expected For Each loop will throw "Collection was modified; enumeration operation may not execute" exception. since on each child form Close() calls, f.MdiChidren collection get modified, that surprised me a lot. Can anybody tell me why it's not throwing that exception?

However it throws "Collection was modified; enumeration operation may not execute" in a client system.

1

There are 1 answers

0
Jay On

If you are running a 64bit OS it could be related to that. I have had issues with that in the past, the work around we had found was to set the project target to x86 (When possible).

Here is a post that has an answer that has a nice explanation of the issue I am talking about.

VS2008 Debugger does not break on unhandled exception

I should mention I have still seen it in 2010 as well.