Results 1 to 10 of 10

Thread: DocumentCompleted fires twice?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Question DocumentCompleted fires twice?

    Hi All,

    I am trying to wait for the webbrowsers page to have finished loading to then carry out the next part of my code.

    However i am having problems because the DocumentCompleted event fires twice?

    I was just wondering why this is happening and if there's anyway to ignore the first event?

    Many thanks,
    Chloe ~X~

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,436

    Re: DocumentCompleted fires twice?

    vb Code:
    1. WebBrowser1.Navigate("http://www.vbforums.com/showthread.php?t=605906")
    2. While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
    3.     Application.DoEvents()
    4. End While
    5. MsgBox("loaded")

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: DocumentCompleted fires twice?

    I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: DocumentCompleted fires twice?

    Hi guys,

    I tried Paul's method and it didn't resolve the issue i'm having, it made the program freeze for a long time.

    This is my code, can you see any problems with it?:

    vb Code:
    1. Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    2.  
    3.         If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
    4.  
    5.             If browserPage.Contains("Recruitment/AdManager/JobList.aspx") Then
    6.                 For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
    7.                     If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
    8.                         input.SetAttribute("checked", "true")
    9.                     End If
    10.                     If input.GetAttribute("name") = "btnCopyJob" Then
    11.                         input.InvokeMember("click")
    12.                     End If
    13.                 Next
    14.             End If
    15.          End If
    16. End Sub

    I want to be able to wait till the second event to then run my code, at the moment it is trying to run my code on the first and second dcoumentCompleted events?

    Many thanks,
    Chloe ~X~

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,436

    Re: DocumentCompleted fires twice?

    does it work?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: DocumentCompleted fires twice?

    Hi Paul,

    What i want to happen is that when that page loads, the checkbox is checked and then a button is clicked.

    At the moment my code is trying to tick the checkbox before the page is loaded and so i get an error from the page in a pop up saying i ahve not ticked the checkbox. If i then click ok on the msgbox the code then ticks the check box correctly and then clicks my button as it should? I just need it to wait a little longer before executing?

    Thanks,
    Chloe ~X~

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: DocumentCompleted fires twice?

    Is the WebBrowser control ever used by the User? or are you just using its methods to login in a site?

    If the latter is true, then I would suggest ditching the WebBrowser control altogether and just use HTTPWebRequest.

    You can accomplish the same things without the overhead of the WebBrowser control.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: DocumentCompleted fires twice?

    Hi,

    Yes the webbrowser is being used by the user, the idea is i'm automating part of the task the user has to do manually?

    Thanks,
    Chloe ~X~

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Re: DocumentCompleted fires twice?

    Turns out it was my code :S!

    When i seperated the action like below it worked fine:

    vb Code:
    1. For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
    2.                     If input.GetAttribute("id") = "rptResults_ctl01_chkAction" Then
    3.                         input.SetAttribute("checked", "true")
    4.                     End If
    5.                 Next
    6.  
    7.                 For Each input As HtmlElement In Me.WebBrowser1.Document.GetElementsByTagName("input")
    8.  
    9.                     If input.GetAttribute("name") = "btnCopyJob" Then
    10.                         input.InvokeMember("click")
    11.                     End If
    12.                 Next

    Thanks as always for your responses.

    Chloe ~X~

  10. #10
    New Member
    Join Date
    Oct 2010
    Posts
    8

    Re: DocumentCompleted fires twice?

    Quote Originally Posted by jmcilhinney View Post
    I have to disagree with Paul's suggestion. "Busy waiting" loops like that are never a good idea. If the DocumentCompleted event is raised twice it's because two documents are loaded. Most likely there's a frame in the main page. You can still test the ReadyState property but you do it in the DocumentCompleted event handler. It will be Complete the second time it's raised.
    Just wanted to comment -- I found this solution via Google and it fixed a problem I've been having wit VB for ages. Thanks, jmcilhinney!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width