Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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;
}

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.
Posted
Updated 11-Jun-10 4:43am
v5

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.
 
Share this answer
 
v3
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();
 
Share this answer
 
v2
Comments
bruccutler 11-Jun-10 10:24am    
I think what I will do is separate this code into its own module and see if it works. It used to work for me as well, but it stopped working. I think I will break it out and try again.

Thanks for your help.
Add this after p1.Start()

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


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

e.Handled = true;
 
Share this answer
 
Comments
bruccutler 14-Jun-10 17:57pm    
The EventArgs doesn't have a parameter called Handled, so I can't do this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900