Process.Kill() issue

[亡魂溺海] 提交于 2019-12-22 10:35:22

问题


I have the following lines of code:

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

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

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

Cannot process request because the process has exited.

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

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


回答1:


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.




回答2:


I didnt get the same issues as you

I found that the following worked if the process was still running or not

    Process p = Process.Start("notepad.exe", "");
    Thread.Sleep(5000);
    if (!p.HasExited) p.Kill();

It only complained IF the window had been closed. Hence the check



来源:https://stackoverflow.com/questions/11930914/process-kill-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!