C #: How to resolve multiple invocations of a webbrowser.documentcompleted event

Source: Internet
Author: User

On the DocumentCompleted event, MSDN explained that the document was executed after loading, but in my program documentcompleted was repeatedly called, check the information, there are probably the following situations.


1, WebBrowser load a page documentcompleted event will be executed two times, but these two times readystate state is not the same, respectively, intercative and complete. MSDN's interpretation of these two status values is complete the control has finished loading the new document and all its contents; Interactive the control has loaded enough documents to allow limited user interaction, such as clicking a hyperlink that has been displayed. This shows that, in fact, the DocumentCompleted event will be called once in the interactive state and the complete state, then we can determine exactly what we need to do according to our needs. The workaround instance code is as follows:

if (webbrowser1.readystate!=webbrowerreadystate.complete)     return

2. If a page contains multiple frames pages, it is also possible to trigger a documentcompleted event when each frame page is loaded (MSDN explains that it is documentcomplete to get triggered multiple times in the case of multiple frames. Not every framework will trigger this event, but each frame that triggers the Downloadbegin event will trigger the corresponding DocumentComplete event. In this case the workaround instance code is as follows:

if (e.url.tostring ()! =webBrowser1.Url.ToString ()    ) return;

In addition, MSDN provides a different scenario, as described in:

3, for the above two cases I did not meet, but encountered a third situation: In my documentcompleted incident executed the corresponding code, the result triggered a documentcompleted event, so cyclical, endless. Use Webbrowser1.stop (), still can't stop. Webbrowser1.nagviate ("About:blank") is still the problem. At this point we can only unload the DocumentCompleted event, that is, after executing the code you need in the DocumentCompleted event, another sentence:

New Webbrowserdocumentcompletedeventhandler (webbrowser1_documentcompleted);

Reprinted from: http://www.cnblogs.com/rchlz/archive/2009/02/27/1399274.html

Related articles

WebBrowser. DocumentCompleted Events

WebBrowser class

WebBrowser. URL Properties

WebBrowser. Navigate Method

C #: How to resolve multiple invocations of a webbrowser.documentcompleted event

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.