Why is WebBrowser_DocumentCompleted() firing twice?

前端 未结 9 1214
独厮守ぢ
独厮守ぢ 2020-11-29 08:35

Well, I\'m using a simple webbrowser control to browse to a page, so I need to change the Text of the form while doing so. I\'m using -

private void webBrow         


        
相关标签:
9条回答
  • 2020-11-29 08:54

    If firing twice is a problem then this should work:

      string body="";
    
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (body == webBrowser1.Document.Body.InnerHtml) return;
            body = webBrowser1.Document.Body.InnerHtml;
    
            // Here is something you want
        }
    
    0 讨论(0)
  • 2020-11-29 09:06

    Actually, it doesn't always get fired. Haven't figured out why not. I have a timer and just check the ReadyState repeatedly for a few minutes. (Using embedded browser control).

    0 讨论(0)
  • 2020-11-29 09:10

    Every time a frame loads, the event is fired.

    Also, before you even go there, the IsBusy property will only be True whilst the first frame has not loaded.

    void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
      if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
        return; 
    
      //The page is finished loading 
    }
    
    0 讨论(0)
  • 2020-11-29 09:12

    How To Determine When a Page Is Done Loading in WebBrowser Control DocumentCompleted is WinForms' wrapper of the DocumentComplete evert, however WebBrowserDocumentCompletedEventArgs hides the sender parameter so you cannot tell which frame is raising the event. Alternatively you can check WebBrowser.ReadyState.

    0 讨论(0)
  • 2020-11-29 09:14

    You can check the WebBrowser.ReadyState when the event is fired:

    if (browser.ReadyState != WebBrowserReadyState.Complete)
        return;
    

    ReadyState will be set to Complete once the whole document is ready.

    0 讨论(0)
  • 2020-11-29 09:16

    Might be you are subscribing this event multiple times like in your some method when your navigating to URL everytime you subscribe to this event.

    To solve this problem, move that line out of the method and put it somewhere else where it will only be called once per instance. In the constructor of the class perhaps... That should solve your problem .

    0 讨论(0)
提交回复
热议问题