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