I am using BrowserControl to navigate to range of web pages on a site and then parse the html and extract information about books etc… I am having problems related (I think) to threading…
I have something like this.
// MAIN LOOP
for (int i = 0; i < NumberOfPages; i++)
{
WebBrowser.Navigate("http://AWebSite/" + NumberOfPages.ToString());
}
// HANDLE ON_LOADED EVENT
void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
// Retrieve HTMLDocument, Parse it etc
}
Now since it takes a few seconds for the event to fire after the control navigates to a page, I have one of two options:
OPTION1 Wait a few seconds in my main loop, like this:
for (int i = 0; i < NumberOfPages; i++)
{
WebBrowser.Navigate("http://www.mysite.com");
// wait for 5 seconds
DateTime wait = new DateTime();
while (new DateTime().Ticks < wait.Ticks + 5000)
{
// not sure if I need do events here
}
}
OPTION2 Another idea is to a Global Variable as a (Boolean) Flag to indicate to the event handler that the page is still downloading (the flag is set to busy in the main look and then reset and then reset after after handling the html returned).
I have a feeling both of these approaches are clumsy and really that there is a better way is to somehow handle these two things (running on different threads?)