检查所引用的类对象是否已处于活动状态 [英] To check if a referenced class object is already active or not

查看:67
本文介绍了检查所引用的类对象是否已处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Windows应用程序项目.我在另一个项目中引用了一个项目,并为所引用的项目类创建了一个实例(对象).

第一个项目在第二个项目中引用.

场景:
第二个项目是我的启动项目.当我单击第二个项目中的按钮时,第一个项目的表单将被打开[obj1.show(); ].因此,在这时,这两种形式都将处于活动状态.

问题:
当我单击第二个项目表单中的另一个按钮时,是否可以检查我的第一个项目表单是否已打开或关闭?

请帮助我.

I have two windows application projects. I referenced one of the project in the other project and created an instance(object) for the referenced project class.

First project is referenced in the second project.

Scenario:
Second project is my start-up project. When I click a button in my second project, the first project''s form will get opened [ obj1.show(); ]. So at this point of time, both the forms will be active.

Question:
When I click another button in the second project''s form, is there a possibility to check if my first project''s form is already open or closed ?

Kindly help me out.

推荐答案

在打开表单时,它会保留对打开的表单的引用,并将处理程序添加到 FormClosed事件 [ ^ ]发生时,清除引用.

还有其他方法-例如,您可以查看Windows窗口列表-但这样做很简单!


请您介绍一下这种方法.
谢谢."



在类级别声明表单引用:
The way I would do it it to keep a reference to the form you opened when you open it, and add a handler to the FormClosed event[^] When it occurs, clear the reference.

There are other ways - you can look though the Windows window list for it for example - but it''s a lot simpler to do it this way!


"Could you please brief me with this method.
Thanks."



Declare the form reference at class level:
private MyForm openedForm = null;


然后,当您要打开它时:


Then, when you want to open it:

if (openedForm == null)
    {
    openedForm = new MyForm();
    openedForm.FormClosed += new FormClosedEventHandler(openedForm_FormClosed);
    ...
    }

处理程序很简单:

And the handler is trivial:

void openedForm_FormClosed(object sender, FormClosedEventArgs e)
    {
    openedForm = null;
    }


如果两个项目都是可执行文件(* .exe),则可以使用该名称搜索正在运行的进程. System.Diagnostics.Process是要使用的名称空间.请注意,该进程可能属于同一计算机上登录的其他用户-请检查该进程的所有者.
如果您要确保仅运行一个应用程序实例,请使用例如Microsoft的应用程序框架.它允许您将命令作为命令行参数发送到正在运行的应用程序.
In case that both projects are executables (*.exe), you can search for a running process with that name. System.Diagnostics.Process is the namespace to use. Note that the process may belong to a different user logged in on the same computer - check the owner of the process.
If you want to make sure that only one instance of your application is running, use e.g. Microsoft''s Application Framework. It allows you to send commands as comand line parameters to the running application.


这篇关于检查所引用的类对象是否已处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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