WebBrowser controls to determine the simple way the Web page is finished loading

Source: Internet
Author: User

in general, when the ReadyState property becomes Readystate_complete, the WebBrowser control instructs the Web page to load by triggering the DocumentCompleted event. However, when the loaded Web page contains a frame, the event may be triggered more than once, so it is not easy to judge that the page is loaded.

From the official website of Microsoft, not every frame corresponds to a documentcompleted event, and only the frame that triggered the Downloadbegin event has a corresponding DocumentCompleted event. In addition, the outermost frame is always the last to trigger the DocumentCompleted event. The DocumentCompleted event has an IDispatch * type parameter that indicates the event that is fired on which frame. So, to determine if the document is loaded, just determine if the IDispatch * parameter is the IDispatch of the WebBrowser control.

A description of the issue on the Microsoft Support website:
http://support.microsoft.com/?scid=kb%3Ben-us%3B180366&x=9&y=14

This method uses COM-related knowledge, in the absence of ready-made code, for things like mine. NET began to enter the Windows platform of programmers to understand and implement it has a certain degree of difficulty. In particular, the WebBrowser control under. NET 2.0 is an incomplete encapsulation of webbrowser COM components, and I'm looking at. NET 2.0 IDispatch parameters or related parameters were not found after the WebBrowserDocumentCompletedEventArgs of WebBrowser. It is not known whether it was omitted during the encapsulation process.

I hope that a master can tell me how to implement the above method in C #, thank you first! However, using the outermost frame mentioned above is always the last to trigger the DocumentCompleted event, and there is an easier way to do it:

1. Introduce a counter variable with an initial value of 0;
2. Add one to the counter when the Downloadbegin (navigated in. NET 2.0) event occurs in the WebBrowser control;
3. Subtract one from the counter when the downloadcompleted event of the WebBrowser control occurs;
4. The counter is reduced to 0 to indicate that the document has finished loading

Main code:

// counter
  int counter = 0;



// Add an event response function
 This . webbrowser.navigated + = new System.Windows.Forms.WebBrowserNavigatedEventHandler (webbrowser_navigated);

this. webbrowser.documentcompleted + = new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler ( This. webbrowser_documentcompleted);

// Event response Function
  private void webbrowser_navigated ( object sender, WebBrowserNavigatedEventArgs e)
{
Counter + + ;


private void webbrowser_documentcompleted ( object sender, WebBrowserDocumentCompletedEventArgs e)
{
     COUNTER --&NBSP,
      if    ( 0   ==   counter)
      {
       //   loading complete  
      }  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.