c# - 使用 Process.Start 运行时如何关闭 Internet Explorer?

标签 c# internet-explorer process

我有一个 WPF 应用程序正在使用 Process.Start 方法启动 Internet Explorer(版本 9,在 Win7 X64 上)。

我保存 ProcessID 以便在应用程序关闭时将其关闭。但是,当应用程序退出时,Internet Explorer 在任务管理器中仍然可见。

这是我使用的代码:

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();
        }
    }
}

我仔细检查过,对 _process.Close() 的调用实际上已经完成。

什么会导致这种行为?

PS:我怀疑这是由于 Internet Explorer 内部造成的。运行exe不需要创建新进程,但可以使用一些IPC来控制其他实例。我使用 -noframemerging 命令行参数,但它看起来并不能解决问题。

[编辑] 这是另一个 question I asked few days ago 的延续.实际上,我正在调用 SetParent 函数以将生成的 IE 嵌入到我的应用程序中。我无法使用 WebBrowser 控件,因为它不支持我正在寻找的隔离。所以当我的应用程序关闭时关闭 IE 是可以的。

最佳答案

Internet Explorer 的每个选项卡都是一个进程。如果您打开带有多个选项卡的 IE 或用户打开另一个选项卡,则不足以终止进程。

但是

_process.Close();

释放属于_process 的所有资源。您可以使用 _process.Kill() 方法代替它。

关于c# - 使用 Process.Start 运行时如何关闭 Internet Explorer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346533/

相关文章:

java - Internet Explorer 不支持 PongWebSocketFrame

c++ - 如何获取进程状态(正在运行、已终止)事件?

c# - 将 Int[] 数组添加到 List<int[]>

c# - 在字典中创建自定义键

css - fiddle 工作,真实页面不在 IE9 中

javascript - 如何删除默认图像图标

java - 难道每个线程都不需要自己的 JVM 副本吗?

memory - 进程和Golang中的Goroutine一样吗?

c# - 用户 'IIS APPPOOL\myAppPoolName' 登录失败,无法为此添加新用户

c# - 为什么我的字符串中添加了一些意外的符号?