I'm using the WebBrowser control within a UserControl that I have authored. I am having issues with setting the DocumentText property of the WebBrowser control. Using a loop comparing the value to the desired set value with a Thread.Sleep() as suggeszted by the MSDN article on the DocumentText property never terminates. This is occuring in the MyUserControl constructor as part of after InitializeComponent().
if (webBrowser1.Document != null)
{
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(htmlDoc);
}
else
{
webBrowser1.DocumentText = htmlDoc;
}
while (!webBrowser1.DocumentText.Equals(htmlDoc)) //This is the loop that never terminates. htmlDoc has the appropriate value.
{
System.Threading.Thread.Sleep(100);
}
What forum should I post in to get some help on this? | | OdysseyBMoran Monday, October 05, 2009 7:44 PM | The problem is that the WebBrowser component, being a STA COM component, doesn't actually update internally until the windows message pump is running correctly. The easiest option here, normally, would be to just remove your while loop. As soon as your UserControl loads in the form, the webBrowser will be able to process its messages, and the document should display properly. If that's not an option, you can force it to run by calling Application.DoEvents(). In general, this is a bad idea (for many reasons, easily searchable), but it will make your code run, like so:
if (webBrowser1.Document != null)
{
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(htmlDoc);
}
else
{
webBrowser1.DocumentText = htmlDoc;
}
// This forces the message pump to clear, which allows
// webBrowser1 to update correctly
Application.DoEvents();
while (!webBrowser1.DocumentText.Equals(htmlDoc)
{
System.Threading.Thread.Sleep(100);
}
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byAland LiMSFT, ModeratorWednesday, October 07, 2009 7:30 AM
-
| | Reed Copsey, Jr. Monday, October 05, 2009 8:05 PM | Use the DocumentCompleted event.
Hans Passant.- Marked As Answer byAland LiMSFT, ModeratorWednesday, October 07, 2009 7:30 AM
-
| | nobugz Monday, October 05, 2009 9:16 PM | The problem is that the WebBrowser component, being a STA COM component, doesn't actually update internally until the windows message pump is running correctly. The easiest option here, normally, would be to just remove your while loop. As soon as your UserControl loads in the form, the webBrowser will be able to process its messages, and the document should display properly. If that's not an option, you can force it to run by calling Application.DoEvents(). In general, this is a bad idea (for many reasons, easily searchable), but it will make your code run, like so:
if (webBrowser1.Document != null)
{
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(htmlDoc);
}
else
{
webBrowser1.DocumentText = htmlDoc;
}
// This forces the message pump to clear, which allows
// webBrowser1 to update correctly
Application.DoEvents();
while (!webBrowser1.DocumentText.Equals(htmlDoc)
{
System.Threading.Thread.Sleep(100);
}
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byAland LiMSFT, ModeratorWednesday, October 07, 2009 7:30 AM
-
| | Reed Copsey, Jr. Monday, October 05, 2009 8:05 PM | Your Application.DoEvent() works in Debug mode but not Release mode.... I know debug mode does lots of crazy stuff but I never leave the loop in Release mode.
webBrowser1.DocumentText is always "<HTML></HTML>\0" and never matches the htmlDoc contents. - Edited byOdysseyBMoran Monday, October 05, 2009 8:10 PMClarification of why the loop never terminates.
-
| | OdysseyBMoran Monday, October 05, 2009 8:09 PM | I just tested it. On my system, the above code (with Application.DoEvents()) works perfectly in both debug and release mode. However, I still question the need - why not just leave it out, and let it update once the UI has loaded? There's no reason to force it to try to load in the constructor. It's a bad idea, for many reasons....
Reed Copsey, Jr. - http://reedcopsey.com | | Reed Copsey, Jr. Monday, October 05, 2009 8:21 PM | If I set the DocumentText in the constructor to the default page to display, only "<HTML></HTML>\0" is displayed once the UI is up and running necessitating the forcing of the DoEvent(). After cleaning up my solution's build folders, I think I got it reliably working, however, I you raise lots of warnings for issuing the DoEvent() so any other ideas would be helpful. | | OdysseyBMoran Monday, October 05, 2009 8:25 PM | Use the DocumentCompleted event.
Hans Passant.- Marked As Answer byAland LiMSFT, ModeratorWednesday, October 07, 2009 7:30 AM
-
| | nobugz Monday, October 05, 2009 9:16 PM |
|