如何恢复对已重新启动的进程的引用?
使用 Process.Start,我正在启动各种 IE8 和 Firefox (3.5) 进程,并为其保留一个 Process 实例。
稍后在应用程序中,我将使用 Process 实例的 MainWindowHandle 属性来定位窗口,以便通过 P/Invoke 与某些平台 API 函数一起使用。
但是,IE8 和 Firefox 都会终止我启动的第二进程,然后使用第一个进程重新启动它。 这意味着我的 Process 实例现在引用一个关闭的进程,因此 HasExited 为 true 并且 MainWindowHandle 等于 IntPtr.Zero。
以下是所发生情况的示例(本例中我使用 IE8):
- 使用“iexplore.exe”调用 Process.Start
- 进程启动并继续运行
- Process.Start 使用“iexplore.exe”再次调用
- 第一个进程继续运行,但第二个立即被终止
- 另一个 iexplore 进程启动(大概是由第一个 iexplore 进程启动)。
- 在此期间,只有在第二个进程被杀死并重新启动后,用户才能看到第二个IE窗口。
我理解为什么这些浏览器会这样做,但如果我想稍后控制创建的进程,它确实会给我带来问题。
以前有人遇到过这个问题吗? 您建议如何获取流程参考? 我考虑过使用 GetProcessesByName 并迭代它们的窗口标题,但不幸的是,标题可能与启动的第一个进程相同。
注意: IE8 是第一个使用进程分离的 IE 版本,因此只有 IE8 才会有这种行为,IE7 没有这个问题,不幸的是我也需要 IE8 支持。
Using Process.Start, I am starting various IE8 and Firefox (3.5) processes which I keep a Process instance for.
A little while later in the application, I'll use the Process instances' MainWindowHandle property to target the window for use with some platform API functions via P/Invoke.
However, both IE8 and Firefox will kill the second process I start, then restart it using the first. This means that my Process instance now refers to a closed process, and so HasExited is true and MainWindowHandle is equal to IntPtr.Zero.
Here's an example of what happens (I'm using IE8 for this example):
- Process.Start is called with "iexplore.exe"
- Process starts and continues running
- Process.Start is called again with "iexplore.exe"
- First process continues running, but the second is killed immediately
- Another iexplore process is started (presumably by the first iexplore process).
- During this time, the user sees the second IE window only after the second process is killed and restarted.
I understand why these browsers behave this way, but it does create a problem for me if I want to control the created process later on.
Has anyone come across this problem before? How would you recommend getting a reference to the process back? I thought about using GetProcessesByName and iterating through their window titles, but unfortunately the titles may be the same as the first process launched.
Note: IE8 was the first version of IE to use process separation and so only IE8 behaves this way, I don't have this problem with IE7, unfortuantely I need IE8 support as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我过去曾使用运行对象表(ROT)来查找特定进程并控制它。
http://www.codeproject.com/KB/COM/ROTStuff.aspx
I have used the Running Object Table (ROT) in the past to find a specific process and control it.
http://www.codeproject.com/KB/COM/ROTStuff.aspx