views:

458

answers:

2

When using .NET's Process.Start to run IE8's iexplore.exe, and if you already have another IE8 window open, then the iexplore.exe process that you just started will immediately exit, and its child process will attach to the main IE8 process. The result of this is your Process object will be linked to the "invoker" process that has already exited, but not the child process that is running. Anything you want to do to the Process object will result in an InvalidOperationException telling you the process has exited.

How do I obtain a Process object that is linked to the actual child IE8 process?

A: 

I did it using a non-optimal solution - get a list of all iexplore.exe process IDs before running my iexplore.exe, then get the same list again after iexplore.exe is invoked. Do a diff between the two lists, the extra PID will be the one I've created. This of course won't work in certain cases (other apps or the user started extra iexplore.exe processes while the detection code is running, or IE did some kind of rearrange/recreation of processes as an internal maintenance task).

Jack
Actually I didn't really do that. What I really wanted was the Hwnd not the PID (for controlling this IE). So I got a list of IE's Hwnd before and after the invocation to find the newly created window.
Jack
+3  A: 

As a workaround you may want to force IE to start a new instance (passing -nomerge command line argument):

Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");
Regent