gpt4 book ai didi

c# - 我不想将 Thread.Sleep 与 SHDocVw.ShellWindows 一起使用

转载 作者:太空狗 更新时间:2023-10-30 00:45:43 26 4
gpt4 key购买 nike

我有一个程序,我必须从正在运行的 IE8 进程中获取 SHDocVw.InternetExplorer 实例。要获取实例,请运行以下示例代码。由于某种原因,如果没有 Thread.Sleep,它将无法工作。

如果 Thread.Sleep 被移除,Browser.HWND 会为 m_IEFoundBrowsers 中的所有实例抛出 InvalidCastException。当使用 Thread.Sleep 时,它适用于 IE8 窗口。

有谁知道如何在不使用 Thread.Sleep 的情况下做到这一点? (我不喜欢使用 sleep 功能,通常它只会把问题推到 future ......)

示例代码:

InternetExplorer m_IEBrowser = null;  
ShellWindows m_IEFoundBrowsers = new ShellWindowsClass();
Thread.Sleep(10);
foreach (InternetExplorer Browser in m_IEFoundBrowsers)
{
try
{
if (Browser.HWND == (int)m_Proc.MainWindowHandle)
{
m_IEBrowser = Browser;
break;
}
}
catch(InvalidCastException ice)
{
//Do nothing. Browser.HWND could not execute for this item.
}
}

最佳答案

我发现以下链接似乎支持 Hans 的评论:http://weblogs.asp.net/joberg/archive/2005/05/03/405283.aspx

文章指出:

The Internet Controls Library contains the “ShellWindowsClass” which is basically a collection of all the shell windows (e.g.: IE) spawned across the desktop. That component provides an event handler called “Windows Registered” that we are going to hook up to. Once the process has been launched, we will wait until the corresponding window is registered then we are going to connect our Internet Explorer control to the shell window found. To determine if the window is found, we iterate through the registered windows and we try to find a handle that matches the handle of the process we previously launched. We will use the “ManualResetEvent” synchronization primitive to wait a certain amount of time for the window to be registered.

我希望您能够相对轻松地将这些想法映射到您的问题中。

关于c# - 我不想将 Thread.Sleep 与 SHDocVw.ShellWindows 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4947114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com