How to close Internet Explorer when it has been ra

2019-07-15 20:20发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.