0

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.

Riju
  • 566
  • 7
  • 19
  • It sounds like the collection isn't being modified in your test environment. The fix for live environment would be to add forms to a separate list first and loop over that list closing them. – JonAlb Nov 01 '11 at 09:25
  • MdiChildren returns an *array*, not a collection. You are not close to diagnosing your user's crash. – Hans Passant Nov 01 '11 at 10:15
  • @JonAlb: i don't think that is the case, i used break point and verified that child form collection is size getting reduce in each iteration. – Riju Nov 01 '11 at 10:17
  • @ Hans Passant: The function that throw exception contain only that much code with a try.. catch. – Riju Nov 01 '11 at 10:47

1 Answers1

0

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.

Community
  • 1
  • 1
Jay
  • 5,897
  • 1
  • 25
  • 28