2

I'm have a WPF application that is starting Internet explorer (Version 9, on Win7 X64) using Process.Start method.

I save the ProcessID in order to close it when the application is closed. However, when the application exits, the Internet Explorer is still visible in the task manager.

Here is the code I'm using :

public class MyClass : IDisposable
{
    Process _process;


    public void Go()
    {

        ProcessStartInfo psi = new ProcessStartInfo { 
            FileName = "iexplore.exe", 
            Arguments = "-noframemerging -private \"http://whathaveyoutried.com\"" 
        };
        _process = Process.Start(psi);
        _process.WaitForInputIdle();
    }

    private void Close(object sender, RoutedEventArgs e)
    {
        if (_process != null)
        {
            _process.Refresh();
            _process.Close();
        }
    }

    public void Dispose()
    {
        if (_process != null)
        {
            _process.Refresh();
            _process.Close();
        }
    }
}

}

I double checked, the call to _process.Close() is actually done.

What can cause this behavior?

PS: I'm suspecting this is due to Internet Explorer internal. Running the exe won't necessary create a new process, but can use some IPC to control other instances. I use the -noframemerging command line argument, but it does not look to solve the issue.

[Edit] This is the continuation of another question I asked few days ago. Actually, I'm Pinvoking SetParent function to embbed the spawned IE in my application. I can't use the WebBrowser control because it does not support the isolation I'm looking for. So it's OK to close IE when my app closes.

Community
  • 1
  • 1
Steve B
  • 36,818
  • 21
  • 101
  • 174
  • If you launch a visible application, it belongs to the user, not your application (e.g. they might open new tabs in there). If you want a restricted browser, use some kind of browser control within a window belonging to your app. – Damien_The_Unbeliever Jul 05 '12 at 14:43
  • There's [a bit of background behind](http://stackoverflow.com/questions/11186817) this requirement. Basically, I can't use the WebBrowser control, so I start a new IE and attach its windows in my app using SetParent. This is working, a bit ugly, but working. The only drawback is that I don't know how to actually keep track of the actual process, and it leads to ghost processes in memory. But you are right. In a 'standard' scenario, this would be a bad practice :) – Steve B Jul 05 '12 at 14:48
  • 3
    As I said - another drawback is that the user starts treating that IE instance like any other (they may not be able to tell them apart), and opens new tabs, pointing at pages that are nothing to do with you. the user *does not* expect that exiting your application will kill one of their IE windows. – Damien_The_Unbeliever Jul 05 '12 at 14:50

3 Answers3

0

Every tab of Internet Explorer is a process. If you open IE with multiple tabs or user opens another tabs, it won't be enough to kill process.

But

_process.Close();

Frees all resources belongs to _process. You can use _process.Kill() method instead of it.

Mehmet Ali Sert
  • 184
  • 2
  • 9
  • This actually kills the instance of Internet Explore. I'm a bit uncomfortable with this technique because I'm not sure Killing a process, versus telling it to shutdown properly, won't have side effects. – Steve B Jul 05 '12 at 15:06
  • _Process.Close() eventuall calls _Process.Kill() so I don't see the difference. Have you even tried, to see if it might work, I mean your already in "I am using a hack" to do what you want. – Security Hound Jul 05 '12 at 15:30
0

For better security and stability, IE8 uses the Loosely Coupled Internet Explorer (LCIE) architecture and runs the browser frame and tabs in separate processes. LCIE prevents glitches and hangs from bringing down the entire browser and leads to higher performance and scalability. I read this on Wikipedia.

You can disable LCIE, but I am not sure why you would want to do that. I would consider using the a solution that @Damien_The_Unbeliever mention above.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
  • I won't disable the LCIE or whatever can change the user configuration. As I said to @damien_the_unbeliever, I can't use the Web Browser Control, for a more complex problem. FYI, I use the -noframemerging to ensure IE starts in a new process (a new session actually). Maybe there is way to ask this specific IE instance to close all of its tabs ? – Steve B Jul 05 '12 at 15:00
  • Now you know why the process id is not the same, you should ask a new question. How do I get/find the process id of a particular tab in IE9? – Black Frog Jul 05 '12 at 15:15
0

I have an IE application in my win32 Application and i'm using Win32Api - SendMessage with WM_CLOSE = 0x0010

SendMessage(handle,0x0010,IntPtr.Zero, IntPtr.Zero)}

This closes the IE, how ever you need to have the specific handle of the Browser (Class IEFrame).

There is one drawback, in case you have more then one tab, the IE opens confirm close dialog which prevent the close process.

One way to overcome the dialog opening is to set it check box to (always close), but for me itţs not an option.

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164