如何以编程方式打开应用程序并检查它何时关闭?
我已经能够使用 winforms 应用程序打开另一个 winforms 应用程序:
Rhino4.Application oRhino = (Rhino4.Application)Activator.CreateInstance(Type.GetTypeFromProgID("Rhino4.Application"));
但如何检查它是否关闭呢?是否可以创建一个在用户关闭应用程序时触发的事件?
编辑
Process[] pr = Process.GetProcessesByName("Rhino4");
for (int i = 0; i < pr.Length; i++)
{
if (pr[i].MainWindowTitle != null)
{
if (pr[i].MainWindowTitle.Length > 4)
{
if (pr[i].MainWindowTitle.Substring(0, 4) == "wall")
{
pr[i].Exited += new EventHandler(caseInfoMenu_Exited);
}
}
}
}
void caseInfoMenu_Exited(object sender, EventArgs e)
{
MessageBox.Show("Window closed");
}
我已经设法使用此代码识别该过程。但是当我关闭程序时,退出事件不会触发。
I've been able to use a winforms application to open another winforms application using:
Rhino4.Application oRhino = (Rhino4.Application)Activator.CreateInstance(Type.GetTypeFromProgID("Rhino4.Application"));
But how do I check if it gets closed? Is it possible to create an event that will get fired when the user closes the application?
EDIT
Process[] pr = Process.GetProcessesByName("Rhino4");
for (int i = 0; i < pr.Length; i++)
{
if (pr[i].MainWindowTitle != null)
{
if (pr[i].MainWindowTitle.Length > 4)
{
if (pr[i].MainWindowTitle.Substring(0, 4) == "wall")
{
pr[i].Exited += new EventHandler(caseInfoMenu_Exited);
}
}
}
}
void caseInfoMenu_Exited(object sender, EventArgs e)
{
MessageBox.Show("Window closed");
}
I've managed to identify the process using this code. But the Exited-event doesn't fire when I close the program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能不是最优雅的解决方案,但您可以通过检查进程是否存在来间接做到这一点,然后重复执行此操作。当然,如果您还没有掌握该流程的话。
编辑:阅读阿卜杜勒的回答中的帖子以及您自己的问题后对此的一些想法。这绝不是一个答案,但也许它可以帮助您完成任务。
首先,Activator.CreateInstance 对您提供给它的对象类型调用最合适的构造函数,并返回该对象的句柄。它本身创建线程/进程,因此它不了解它们。您将在列表中看到的 (9) 个进程可能是由 Rheno4 类本身创建的。有一个关于此的讨论 在这里。
其次,根据msdn,EnableRaisingEvents属性应该创建进程时将其设置为 true 以使 Exited 事件正常运行。这让我想知道当您在流程创建后附加事件时会发生什么?
当然,您可以在调用 CreateInstance 之前和之后迭代所有匹配进程,以提取已创建的所有新的 Rheno4 实例。但这远非万无一失的解决方案,风险在于您正在获取由其他人创建的进程,或者并非所有进程都被检索(以防创建其他对象出现延迟)。然而,根据您的需要,这可能是适用的。
另一个想法。从 GetProcessesByName 返回的进程具有一组丰富的属性。也许您可以仔细查看这些内容并找到返回的进程的共同点。我要开始研究的是:Threads、StartInfo、MainModule。
Its maybe not the most elegant solution but indirectly you could do this by checking if the process exist or not and then do this repeatable. This is of course if you do not already have a handle to the process.
Edit: Some thoughts on this after reading the posts in Abdul's answer plus your own question. This is by no means an answer, but maybe it can help you on your quest.
Firstly, Activator.CreateInstance calls the best fitting constructor on the object type that you give to it and returns a handle to that object. It does create the threads/processes itself and thus it has not knowledge about them. The (9) processes you'll see in your list are probably created by the Rheno4 class itself. There is a discussion about this here.
Secondly, according to msdn the EnableRaisingEvents property should be set to true when the process is created for the Exited event to function correctly. This leaves me wondering what happens when you attach the event after the process is already created?
You could of course iterate over all matching processess before and after calling CreateInstance to extract all new instances of Rheno4 that has been created. But this is far from a bulletproof solution and the risk is that you are fetching processes that are created by someone else or that not all processes are retreived (in case there is a delay in creating the other object). Depending on your needs, however, this maybe is appliable.
Another thought. The processes returned from the GetProcessesByName has a rich set of properties. Maybe you can look though these and find a common denominator for the processes returned. The ones I would start to investigate are: Threads, StartInfo, MainModule.
怎么样?
捕获
Exited
事件源 msdn。What about catching
Exited
eventSource msdn.
如果您想在关闭后再次启动应用程序,则:-
我认为您需要创建一个 Windows 服务,它将继续检查正在运行的进程,如果它已关闭,则再次启动应用程序
就事件而言,Windows 应用程序中存在“关闭”和“关闭”事件,当用户关闭应用程序。
If you want to start the application again after closing then :-
I think you need to crearte a Windows Service which will keep checking the process running and if it is closed then start the application again
As far as events are concern then "Closing" and "Close" events are there in Windows App which fires when user shutdowns the app.