Determine if a Form from another project is open

自古美人都是妖i 提交于 2019-12-10 23:32:05

问题


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?

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

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.

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

Any help is appreciated.


回答1:


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;
        }
    }
}



回答2:


What you need is a way of Inter Process Communication.
There are many ways to achieve this most of the will be overkill for your situation.
I this the best way is your case is to have a file that the admin process will write to and the other processes will read from and infer the state of the admin process.



来源:https://stackoverflow.com/questions/3265651/determine-if-a-form-from-another-project-is-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!