1

how to get the DocResult after _web_ProgressChanged has been operating?

public class WebHelper
{
    WebBrowser _web = null;

    public string DocResult = string.Empty;
    public WebHelper(string url)
    {
        _web = new WebBrowser();
        _web.ProgressChanged += _web_ProgressChanged;
        _web.Navigate(url);
    }

    void _web_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        DocResult = _web.Document.Body.InnerHtml;
    }
}
class MyWork
{
    public string GetDocContent(string url)
    {
        WebHelper wh = new WebHelper(url);
        return wh.DocResult;
    }
}

how to get the DocResult after _web_ProgressChanged has been operating?

mleko
  • 11,650
  • 6
  • 50
  • 71

1 Answers1

2

how to get the DocResult after _web_ProgressChanged has been operating?

You don't. ProgressChanged stopped being useful to give any feedback at least a decade ago. It still made somewhat sense back in the 1990s when browsers were connected to the Internet service provider through a low-speed telephone modem. And web pages were still very stark, very little markup and styling, so the user didn't have to wait for minutes for a web page to load. ProgressChanged was useful then to drive a progress bar to indicate how much longer he had to wait.

That's over and done with, web pages today are built up with many data transfers. Easily a dozen or more, totaling hundreds of kilobytes. Some transferring data from the web server itself, some from a CDN, some from a 3rd party server for things like analytics. Some are html, some are css, some are javascript, some are images. Each tickling ProgressChanged, it wildly changes while a web page is busy loading. Browsers don't show progress anymore, just a spinner.

There is no connection between the ProgressChanged event and the state of the DOM, there never was one. You must use the DocumentCompleted event before attempting to use the Document property.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • You do not understand the meaning of the operations in this code. And I understand the meaning of what you have said. But I just need to get the content after the event is done. You can see this example and you will understand what I mean. [DemoWebHelper Project](https://mega.co.nz/#!T543XZAD!YnHrbNiGqaAAoLvq14_94XoPZNl3ahFg8X-SMyA5t74) – Tuan Nguyen Aug 17 '14 at 08:56
  • Thank @Hans Passant. I have found how fixed it with 'while (_wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }' – Tuan Nguyen Aug 17 '14 at 09:16
  • That is [a bug, not a fix](http://stackoverflow.com/a/5183623/17034). Use the DocumentCompleted event. – Hans Passant Aug 17 '14 at 09:49
  • Are you sure using class WebHelper to get DocResult and insert it into rtbOther control. I have changed event ProgressChanged to DocumentComplete but DocResult is empty. If you Navigate an search uri(http://google.com.vn/#q=some+keywork) from webBrowser control with event DocumentComplete do not get a list search result for you. It is get document of google home page. Events Navigated, DocumentComplete, ProgressChanged - only after ProgressChanged event is done...it is get list search result from google to you. – Tuan Nguyen Aug 17 '14 at 11:45