By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

Problem loading a WebBrowser in a separate thread

P: n/a
I'm trying to parse html that resides locally by using the HtmlDocument class
and unfortunately you can only get an instance of an HtmlDocument through the
WebBrowser control.

Some of the html files I want to parse are quite large so I want to get the
HtmlDocument in a separate thread. But for some reason, whenever I move the
code to navigate the WebBrowser to a separate thread the DocumentCompleted
event is never fired. When I step through I can see that some of it is
loading but not all. Here is some code:
-----------
....
//Start the thread
Thread worker = new Thread(new ParameterizedThreadStart(LoadHtml));
worker.SetApartmentState(ApartmentState.STA);
worker.Start(files);
....

private void LoadHtml(object obj)
{
foreach(FileInfo fileinfo in (FileInfo[])obj)
{
//Create a new chapter and add it to the list of chapters
HtmlParser parser = new HtmlParser(fileinfo);
m_listHtmlParsers.Add(parser);
}
}

public class HtmlParser
{
public HtmlParser(FileInfo fileinfo)
{
//Set up our WebBrowser Control, which will Parse the HtmlDocument
//that it contains once the DocumentCompleted Event is fired
m_wbHtmlParser.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(ParseHtml) ;
m_wbHtmlParser.Navigate(m_fiChapterFile.FullName);
}

private void ParseHtml(object sender,
ebBrowserDocumentCompletedEventArgs e)
{
//We never get here
}
}

------------------
If I take this out of the thread, the DocuementCompleted event get's fired
and everything works. I think what is happening is that the thread is being
exited before the document is completely loaded. But I'm not sure how to
make sure all the HtmlParsers have fired the DocumentCompleted event before
the thread ends.

Any help would be greatly appreciated.
Oct 13 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.