如何检查Windows Forms Instance是否运行? [英] How to check windows Forms Instance running or not ?
本文介绍了如何检查Windows Forms Instance是否运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的,
请,有人能告诉我如何检查Windows窗体实例当前正在运行吗?我有Button显示第二个窗体,但是需要知道它是否已打开?
Dears,
please, Can anyone tell me how to check Windows forms instance is running at presently?I have Button to display second form,But need to know its already opened or not ?
推荐答案
要阻止多个实例,您可以查看此 [此 [ VB.NET中的单实例应用程序中的另一个有用链接 [
To block mulitple instances, you can have a look at this[^]. The code is in C#, but you might be able to convert it to VB.Net.
If the form is a form (application) you coded, you can control the number of instances by using code similar to this[^].
Another useful link at Single Instance Application in VB.NET[^].
有两种方法:
您可以在当前的表单类中保留对它的引用,并检查:
There are a couple of ways:
You could keep a reference to it in your current form class, and check that:
private MyForm myForm = null;
private void OpenNewMyFormInstance()
{
if (myForm == null)
{
myForm = new MyForm();
myForm.FormClosed += new FormClosedEventHandler(myForm_FormClosed);
myForm.Show();
}
}
void myForm_FormClosed(object sender, FormClosedEventArgs e)
{
myForm = null;
}
您需要处理FormClosed事件以注册用户已将其关闭.
或者,您可以检查现有实例:
You need to handle the FormClosed event to register that the user closed it.
Or, you could check for an existing instance:
bool isMyFormOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f is MyForm)
{
isMyFormOpen = true;
break;
}
}
if (!isMyFormOpen)
{
MyForm f = new MyForm();
f.Show();
}
我?我会保留该引用,因为我可能想将其用于其他用途.
Me? I''d keep the reference, because I might want to use it for something else.
这篇关于如何检查Windows Forms Instance是否运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文