为什么它不抛出“集合已修改;”枚举操作可能无法执行”例外

发布于 2024-12-13 05:08:54 字数 540 浏览 6 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄意 2024-12-20 05:08:54

如果您运行的是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文