Reputation: 2118
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?
Upvotes: 3
Views: 3614
Reputation: 106796
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.
Upvotes: 4
Reputation: 17858
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
Upvotes: 0