为什么它不抛出“集合已修改;”枚举操作可能无法执行”例外
我的 MDI 应用程序中有一个注销菜单选项。注销时我想关闭所有打开的表单。目前我正在使用以下代码片段来实现此目的;
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
它在我的测试环境中运行完美,即使我预计 For Each 循环 会抛出“集合已修改;枚举操作可能无法执行”< /em> 异常。因为在每个子表单 Close() 调用中,f.MdiChidren 集合都会被修改,这让我很惊讶。谁能告诉我为什么它不抛出异常?
但是,它在客户端系统中抛出“集合已修改;枚举操作可能无法执行”。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您运行的是 64 位操作系统,则可能与此有关。我过去遇到过这个问题,我们发现的解决方法是将项目目标设置为 x86(如果可能)。
这是一篇帖子,其中有一个答案,对我正在讨论的问题有很好的解释。
VS2008 调试器不会因未处理的异常而中断
我应该提到我2010年也仍然看到过它。
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.