应用启动后立即调用Process.Exited事件 [英] Process.Exited event called as soon as app launched

查看:112
本文介绍了应用启动后立即调用Process.Exited事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用Process.Start()以启动iexplore.exe.

I''m calling Process.Start() to launch iexplore.exe.

p1 = new Process();
p1.EnableRaisingEvents = true;
p1.Exited += new EventHandler(p1_Exited);
p1.StartInfo.FileName = "iexplore.exe" /* or word.exe or excel.exe*/
p1.SynchronizingObject = getDataButton;
p1.Start(); 

<br />
void p1_Exited(object sender, EventArgs e)
{
    threadExited = true;
}


我想检测何时关闭iexplore.exe(或word.exe或excel.exe).

发生的事情是,一旦启动iexplore(或Word或Excel),也会调用p1_Exited方法.

我曾经在某个时间进行过此操作,或者大概是这样,但是现在不起作用了.

任何帮助将不胜感激.


I want to detect when iexplore.exe (or word.exe or excel.exe) is closed.

What is happening is that as soon as iexplore (or Word or Excel) is launched, the p1_Exited method is also called.

I had this working at one point, or so I thought, but now it doesn''t work.

Any help would be appreciated.

推荐答案

我要说的是我无法复制它,但是我已经看到了光明.
使用IE,每个窗口都有它自己的进程,并且该窗口中的每个选项卡都有它自己的进程(都称为iexplore),但其中只有一个实例充当其他实例的经理"(原始进程)第一次打开).

这些只是我的假设,但我认为如果您已经有一个IE实例打开并且您启动了一个新实例,则"manager"进程将杀死您的新进程,并生成另一个"child"进程. .

然后的解决方案是检查您是否已打开IE实例,如果已打开,则找出哪个进程是经理"进程.我不知道您如何在不进行进一步研究的情况下如何区分这些过程.

编辑:这是您可以自己做的一项测试.打开任务管理器并启动IE,您应该看到两个iexplore实例,每个新窗口都带来了两个更多的进程,以及每个额外的进程.由于每个窗口都带有两个进程,因此杀死其中一个会导致另一个进程重新启动它(因此,您永远不会没有选项卡或窗口).这个管理器进程是一个例外,它可以(可能)由稍高的内存消耗发现.杀死该iexplore实例也会导致其他所有对象也关闭.

编辑:与其他Microsoft产品似乎也是如此,尽管如果您尝试运行新实例或Word并且现有窗口已存在,则word只会有一个进程(winword.exe).已经打开,则您的进程将立即关闭,现有进程将产生一个新窗口.再次,您需要查看该进程是否已经存在.
I was going to say that I couldn''t reproduce it, but I have seen the light.

With IE each window has it''s own process and each tab in that window has it''s own process (all called iexplore) but only one of those instances acts as a ''manager'' for the others (the original process that was first opened).

These are just my assumptions but I reckon if you already have an instance of IE open and you start a new one then the ''manager'' process kills your new process and spawns another of it''s ''child'' processes instead.

The solution then is to check if you already have an instance of IE open and if so find out which process is the ''manager'' process. I''ve not any idea how you would distinguish between the processes without looking into it some more though.

EDIT: Here''s a test you can do yourself. Open the Task Manager and start IE, you should see two instances of iexplore, each new window brings in two more processes plus an additional process for each extra. As each windows comes with two processes killing either of them causes the other to restart it (so you are never without a tab or window). The exception being this manager process, which can be spotted by the (possibly) slightly higher memory consumption. Killing that instance of iexplore will cause all other to close as well.

EDIT: It seems to be a similar case with other Microsoft products as well, although word only ever has one process (winword.exe) if you try to run a new instance or Word and an existing window is already open then your process will immediately close and the existing process will spawn a new window. So again, you''d need to see if the process already exists.


嗯,我确实像您所做的那样模拟了方案,没有问题,我与您得到的输出,iexplorer,单词记事本都很好用.


您可以为调用getDataButton()的方法提供代码吗?
Hmm , i did simulate the scenario as you have done and got no problem , i''m confusing with the the out put you got , iexplorer, word notepad all works well.


can you provide the code for the method you called getDataButton();


p1.Start()
之后添加此代码
Add this after p1.Start()

// Wait for Exited event, but not more than 30 seconds.
while (true)
{
    Thread.Sleep(500);
}



然后在p1_Exited方法中,将此行插入该方法的顶部:



And in the p1_Exited method, insert this line at the top of the method:

e.Handled = true;


这篇关于应用启动后立即调用Process.Exited事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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