【问题标题】:How do i check if an instance of the form already exists?如何检查表单的实例是否已经存在?
【发布时间】:2010-09-01 13:18:51
【问题描述】:

我正在为 Rhino 开发一个插件,当我运行命令启动插件时,我执行以下操作。它创建了一个启动表单,上面有一个计时器,2 秒后我加载另一个表单。

如果我再次错误地单击插件图标,它会创建另一个spash 表单实例,它会再次加载插件。

如何防止这种情况发生?

这是制作表单的代码。

public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
            {

                Splash Splash = new Splash();
                Splash.Show();

                return IRhinoCommand.result.success;
            }

【问题讨论】:

    标签: c# winforms plugins rhino


    【解决方案1】:
    public override IRhinoCommand.result RunCommand(IRhinoCommandContext context)
    {
        if (!Application.OpenForms.OfType<Splash>().Any())
        {
            new Thread(() => Application.Run(new Splash())).Start();
        }
        return IRhinoCommand.result.success;
    }
    

    【讨论】:

    • 当我尝试通过 System.Windows.Forms 使用它时找不到 OfType 属性,我该如何使用它?
    • @Bildsoe:如果您使用的是 .NET 3.5 或更高版本,请确保在文件顶部引用了 System.Coreusing System.Linq; 指令。如果不是,请遍历 Application.OpenForms 并检查是否有任何表单是 Splash 对象。
    • @Bildsoe:代码在单独的线程上为表单启动一个消息循环,然后显示它。
    • 好的,我刚刚尝试了代码,表格显示出来,然后很快就消失了。
    • @Bildsoe:好的,只需调用Application.Run(new Splash()); 即可。它将阻塞,直到表单关闭。这是你需要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    相关资源
    最近更新 更多