当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Process.Kill() issue

问题描述:

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?

网友答案:

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.

网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: