简体   繁体   English

如何恢复对已重新启动的流程的引用?

[英]How can I recover a reference to a process that has been relaunched?

Using Process.Start, I am starting various IE8 and Firefox (3.5) processes which I keep a Process instance for. 使用Process.Start,我将启动各种IE8和Firefox(3.5)进程,并为其保留Process实例。

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. 在应用程序中稍后,我将使用Process实例的MainWindowHandle属性将窗口定位为通过P / Invoke与某些平台API函数一起使用。

However, both IE8 and Firefox will kill the second process I start, then restart it using the first. 但是,IE8和Firefox都将终止我启动的第二个进程,然后使用第一个重新启动它。 This means that my Process instance now refers to a closed process, and so HasExited is true and MainWindowHandle is equal to IntPtr.Zero. 这意味着我的Process实例现在引用了一个关闭的进程,因此HasExited为true,MainWindowHandle等于IntPtr.Zero。

Here's an example of what happens (I'm using IE8 for this example): 这是发生情况的一个示例(在此示例中,我使用IE8):

  • Process.Start is called with "iexplore.exe" 使用“ iexplore.exe”调用Process.Start
  • Process starts and continues running 进程开始并继续运行
  • Process.Start is called again with "iexplore.exe" 使用“ iexplore.exe”再次调用Process.Start
  • First process continues running, but the second is killed immediately 第一个进程继续运行,但是第二个进程立即终止
  • Another iexplore process is started (presumably by the first iexplore process). 另一个iexplore过程开始了(大概是第一个iexplore过程)。
  • During this time, the user sees the second IE window only after the second process is killed and restarted. 在此期间,仅在第二个进程被终止并重新启动后,用户才能看到第二个IE窗口。

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. 我考虑过使用GetProcessesByName并遍历其窗口标题,但不幸的是标题可能与启动的第一个进程相同。

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. 注意: IE8是使用进程分离的IE的第一个版本,因此只有IE8才这样运行,IE7没有这个问题,不幸的是我也需要IE8支持。

I have used the Running Object Table (ROT) in the past to find a specific process and control it. 我过去使用过运行对象表(ROT)来查找特定的过程并对其进行控制。

http://www.codeproject.com/KB/COM/ROTStuff.aspx http://www.codeproject.com/KB/COM/ROTStuff.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM