I have a program in which I should get an instance of SHDocVw.InternetExplorer from a running IE8 process. To get an instance, see the sample code below. For some reason, it will not work without Thread.Sleep.
Browser.HWND throws an InvalidCastException for all instances in m_IEFoundBrowsers if Thread.Sleep is deleted. When using Thread.Sleep, it works for IE8 windows.
Does anyone know how to do this without using Thread.Sleep? (I do not like to use the sleep function, usually it just pushes problems into the future ...)
Code example:
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. } }
source share