Determine if a Form from another project is open
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 f开发者_StackOverflow中文版orms 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.
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;
}
}
}
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.
精彩评论