Ok, so here's the deal -- I'm running a Windows Forms WebBrowser control from a service. I know thats a no-no, but it seems to work alright.
The only thing I'm running into a problem with is trying to wait for the browser's pages to load. In a normal application, I'd just do something like
while (browser.readystate != complete) Application.DoEvents()
Obviously this won't work from a service.
I've tried this as an alternative:
public class WebCrawler
{
private class ExposedActiveXWebBrowser : System.Windows.Forms.WebBrowser
{
public SHDocVw.WebBrowser UnderlyingWebBrowser
{
get
{
return ActiveXInstance as SHDocVw.WebBrowser;
}
}
}
ExposedActiveXWebBrowser worker;
public WebBrowserReadyState ReadyState
{
get
{
return worker.ReadyState;
}
}
public HtmlDocument Document
{
get
{
return worker.Document;
}
}
public WebCrawler()
{
worker = new ExposedActiveXWebBrowser();
}
public void Navigate(string urlString)
{
worker.Navigate(urlString);
while (worker.UnderlyingWebBrowser.ReadyState != tagREADYSTATE.READYSTATE_COMPLETE)
Thread.Sleep(0);
}
}
That Navigate method, however, doesn't work. The ReadyState never changes from LOADING.
What I'm wondering is this -- Windows forms WebBrowsers seem to be inherently asynchronous, so does that mean the ActiveX control is already executing on its own thread? Can I, by accessing the underlying activex control through an appropriate interface, just wait for it to complete?