Process.Kill()问题问题、Process、Kill

2023-09-07 03:54:19 作者:千城墨白

我有code以下行:

Process aProcess = Process.Start("ieexplorer", aDummyHTMLFilePath);
//I do nothing in between
aProcess.Kill();

此运行平稳​​,如果有否其他IE窗口中打开。

This runs smooth if there are NO other IE windows open.

但如果有一个窗口打开,我收到了 System.InvalidOperationException aProcess.Kill(); 他说:

But if there is a window open, I get a System.InvalidOperationException on aProcess.Kill(); saying :

无法处理请求,因为该进程已退出。

Cannot process request because the process has exited.

另外,我注意到,在这种情况下, aProcess.HasExited 是在code以上1号线后,真正的权利。

Also, I notice that in this case, aProcess.HasExited is true right after line 1 in the code above.

我怎样才能顺利关闭IE,即使有其他的IE窗口打开?

How can I smoothly close IE, even if there are other IE windows open?

推荐答案

当您启动Internet Explorer的新实例像你这样做会尝试看看如果Internet Explorer已在运行。如果是真的的URL将被打开,已经运行的实例和新实例立即退出。这意味着,当你试图杀死你启动它的进程已自愿退出。但是,你会看到你的屏幕上一个新的浏览器窗口或选项卡,但正在举办由现有的Internet Explorer进程。

When you start a new instance of Internet Explorer like you do it will try to see if Internet Explorer is already running. If that is true the URL is opened in the already running instance and the new instance exits immediately. This means that when you try to kill the process you started it has already exited voluntarily. However, you will see a new browser window or tab on your screen but that is being hosted by the existing Internet Explorer process.