C# Process.Start("IEXPLORE.EXE") 启动后立即触发 Exited 事件..为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1825105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:09:21  来源:igfitidea点击:

Process.Start("IEXPLORE.EXE") immediately fires the Exited event after launch.. why?

c#processinternet-explorer-8

提问by RameshVel

i have a strange problem with IE8 installed in xp. i was trying to launch IE using an System.Diagnostics.Process.Start method in c#. And i have a requirement to trap the exited event of the IE and do some operation. But i ended up in a rather strange problem where the IE immediately fires the exited event after launch.

我在 xp 中安装 IE8 有一个奇怪的问题。我试图在 c# 中使用 System.Diagnostics.Process.Start 方法启动 IE。我需要捕获 IE 的退出事件并执行一些操作。但我最终遇到了一个相当奇怪的问题,即 IE 在启动后立即触发退出事件。

this is the sample code

这是示例代码

     Process objProcess = Process.Start("IEXPLORE.EXE", "http://google.com");

     if (objProcess != null)
    {
        objProcess.EnableRaisingEvents = true;
        objProcess.Exited += new EventHandler(myProcess_Exited);        
    }

    public  static void myProcess_Exited(object sender, System.EventArgs e)
    {
        MessageBox.Show("You exited");
    }

But the above code perfectly works when laucnching different process (ex:notepad) and it fires the exit event when i close the exe.

但是上面的代码在启动不同的进程(例如:记事本)时完美地工作,并且在我关闭 exe 时它会触发退出事件。

this only gives problem launching IE 8. Can someone clarify me what is the problem??

这只会在启动 IE 8 时出现问题。有人可以澄清我是什么问题吗??

UPDATE

更新

Most friends replied my post and saying why you can't just use an URL? why stick with IE?

大多数朋友回复我的帖子并说为什么你不能只使用 URL?为什么坚持使用IE?

here the reason

原因在这里

the ultimate aim of the app is to launch an URL from the windows application and will hide an exe when working on the IE. And show the exe after closing the IE.

该应用程序的最终目的是从 Windows 应用程序启动一个 URL,并在 IE 上工作时隐藏一个 exe。并在关闭 IE 后显示 exe。

Thanks

谢谢

采纳答案by Regent

Most probably is that you have IE already running as a process, so when you try to launch it again as a new process it looks that there are IE running already, tells it that user initiated a new window (so the initial IE will create a "new" window rather than a new one) and exit.

最有可能的是您已经将 IE 作为一个进程运行,所以当您尝试将它作为一个新进程再次启动时,它看起来已经有 IE 正在运行,告诉它用户启动了一个新窗口(因此初始 IE 将创建一个“新”窗口而不是新窗口)并退出。

Possible solution:try starting the process with "-nomerge" command line option:

可能的解决方案:尝试使用“-nomerge”命令行选项启动进程:

    Process objProcess = Process.Start("IEXPLORE.EXE", "-nomerge http://google.com/");

Interesting observation: objProcess.ExitCode(for IE8 at least) will be equal to 0if exited passing control to another instance, and 1if it was actually closed by user.

有趣的观察:(objProcess.ExitCode至少对于 IE8)将等于0if exited 将控制权传递给另一个实例,1如果它实际上被用户关闭。

回答by Thorsten Dittmar

Maybe IEXPLORE itself launches a different process for the URL and ends the process you created? Like a forkon Unix?

也许 IEXPLORE 本身会为 URL 启动一个不同的进程并结束您创建的进程?像fork在 Unix 上?

回答by Eugene Talagrand

If another instance of iexplore.exe is already running on the machine, new instances will connect to that and immediately exit. Also, it's possible that even in the case where iexplore is not running, the multiprocess architectureof Internet Explorer 8 has the parent launch child broker process and exit immediately.

如果 iexplore.exe 的另一个实例已经在机器上运行,新实例将连接到该实例并立即退出。此外,即使在 iexplore 未运行的情况下,Internet Explorer 8的进程体系结构也有可能让父进程启动子代理进程并立即退出。

But these answers are besides the point. You should not be launching Internet Explorer directly. If the user has configured another default browser, they will be unhappy that you are ignoring their preferences. Instead, why don't you try

但这些答案是无关紧要的。您不应直接启动 Internet Explorer。如果用户配置了另一个默认浏览器,他们会因为您忽略了他们的偏好而感到不满。相反,你为什么不试试

System.Diagnostics.Process.Start("http://google.com");

directly and that will do the right thing. You won't be able to tell when the browser closed, but if the command has opened a new tab in an existing browser session for example, the browser close event will be meaningless to your application.

直接,这将做正确的事情。您将无法判断浏览器何时关闭,但例如,如果该命令在现有浏览器会话中打开了一个新选项卡,则浏览器关闭事件对您的应用程序将毫无意义。