确定来自另一个项目的表单是否已打开 [英] Determine if a Form from another project is open

查看:29
本文介绍了确定来自另一个项目的表单是否已打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 c# WinForm 解决方案包含多个项目,包括具有多个表单的管理项目和具有多个表单的用户项目.我希望我的用户表单在特定管理表单打开时表现不同.

My c# WinForm solution contains several projects including an Admin project with several forms and a User project with several forms. I want my user forms to behave differently when specific admin forms are open.

用户表单如何知道管理表单何时打开?

How can the user forms tell when admin forms are open?

所有表单都没有this.Text"值(所有这些值都为空).

All forms have no 'this.Text' value (all these values are null).

当我遍历所有由FormCollection fc = Application.OpenForms"标识的表单时,它不会显示来自其他项目的表单;它似乎只显示来自同一项目的表单.

When I loop through all forms identified by 'FormCollection fc = Application.OpenForms', it does not show the forms from the other project; it seems to only show the forms from the same project.

此外,所有管理表单都从一个 .exe 文件运行,所有用户表单从另一个 .exe 文件运行.

Also, all the admin forms run from one .exe file and all the user forms run from another .exe file.

感谢任何帮助.

推荐答案

使用 该作用域的互斥 类.
Mutex 是一个 Windows 内核对象,它具有 Windows 计算机的唯一标识符.

Use Mutex class for that scope.
Mutex is a Windows kernel object that has an unique identifier for a Windows computer.

public class Form2 : Form
{
    Mutex m;
    protected override void OnShown(EventArgs e)
    {
        base.OnShown(e);
        m = new Mutex(true, "Form2");
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        m.ReleaseMutex();
    }
}

public class Form3 : Form
{
    bool form2IsOpen;
    public Form3()
    {
        try
        {
            Mutex.OpenExisting("Form2");
            form2IsOpen = true;
        }
        catch (WaitHandleCannotBeOpenedException ex)
        {
            form2IsOpen = false;
        }
    }
}

这篇关于确定来自另一个项目的表单是否已打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆