Webbrowser control: A simple method to determine whether a webpage has been loaded

Source: Internet
Author: User
@ Http://www.blogjava.net/weidagang2046/archive/2007/03/04/101785.html

Generally, when the readystate attribute changes to readystate_complete, The webbrowser control triggers the documentcompleted event to indicate that the webpage has been loaded. However, when a webpage contains a frame, this event may be triggered multiple times, so you cannot simply use it to determine that the webpage has been loaded.

I learned from Microsoft's official website that not every frame corresponds to a documentcompleted event. Only the frame that triggers the downloadbegin event will have the corresponding documentcompleted event. In addition, frames at the outermost layer always trigger the documentcompleted event. The documentcompleted event has an idispatch * type parameter, indicating the frame on which the event is triggered. Therefore, to determine whether the document has been loaded, you only need to determine whether the idispatch * parameter is the idispatch of the webbrowser control.

Description on the Microsoft support website:
Http://support.microsoft.com /? SCID = kb % 3ben-us % 3b180366 & X = 9 & Y = 14

This method uses knowledge related to com. Without the ready-made code. net started to enter the Windows platform for programmers to understand and implement a certain degree of difficulty. In particular, the webbrowser control under. NET 2.0 is not fully encapsulated by the webbrowser COM component. I did not find any idispatch parameters or related parameters after investigating the webbrowserdocumentcompletedeventargs of. NET 2.0 webbrowser. I don't know if it is saved in the encapsulation process.

I hope someone can tell me how to use C # To implement the above method. Thank you! However, using the outermost frame mentioned above will always trigger the documentcompleted event. There is also a simpler method:

1. Introduce a counter variable. The initial value is 0;
2. When the downloadbegin (navigated in. NET 2.0) event of the webbrowser control occurs, add one to the counter;
3. When the downloadcompleted event of the webbrowser control occurs, subtract one from the counter;
4. If the counter is reduced to 0, the document is loaded.

Main Code:

// Counter
Int counter = 0;



// Add the 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 --;

If (0 = counter)
{
// Load completed
}
}

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.