Problem with Application.DoEvents() while waiting for WebBrowser to finish loading

柔情痞子 提交于 2019-12-24 01:24:33

问题


I'm trying to load WebBrowser content and after that I want to add some text and scroll to the bottom.

Here's example of my code:

webBrowser1.Url = new System.Uri("file:///" + filePath);
webBrowser1.Document.Body.InnerHtml += text;
webBrowser1.Document.Body.ScrollTop = webBrowser1.Document.Body.ScrollRectangle.Height;

When I run it, there's an unhandled exception "Object reference not set to an instance of an object". Or when I comment line that does the scrolling, then text is added to previous content of the WebBrowser and then navigating to new content.

So after 1st line of my example code I put:

while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();

but it messes up everything. My application is doing really strange things, for example calling the same method many times, when it should be called once.

Is there any solution?


回答1:


I think you actually want to subscribe to DocumentCompleted event.
Application.DoEvents only processes pending Windows message loop items.

In either case, make sure you understand possible drawbacks of calling DoEvents before using it at all.




回答2:


DoEvents() is a bad solution here. You should use explicit thread or BackgroundWorker to load pages and leave UI thread to handle other stuff.



来源:https://stackoverflow.com/questions/6795086/problem-with-application-doevents-while-waiting-for-webbrowser-to-finish-loadi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!