Results 1 to 6 of 6

Thread: Navigate to url and wait.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Navigate to url and wait.

    How do you navigate to a url in a webbrowser and wait for a specific time period like say 30 sec before inputting data into fields in a form? I know how to use WebBrowser1.DocumentCompleted but I don't want to use wait for the complete website to download. There are websites that don't completely download so I don't want to hang up the program.

    I tried using Sleep(30000) but that halts the program, it doesn't load the browser for a specific time period.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Navigate to url and wait.

    You could use a Timer and set its interval value. Then start the timer when the URL is passed to the WebBrowser control and perform your logic in it's Tick Event.

    Although I would first try using the following condition in the DocumentCompleted event:
    VB.NET Code:
    1. If Not (WebBrowser1.Document Is Nothing) Then
    2.     'Your code here
    3. End If
    It is not unusual for the DocumentCompleted event to fire a couple of times when loading a page. The above should make sure that your code doesn't run until the document really has completed.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: Navigate to url and wait.

    Your Timer and set its interval value solution works for one url but how do I loop several urls. When I test the timer, the commands doesn't stop to wait for the timer. For example,

    WebBrowser1.Navigate(TextBox1.Text)
    Timer1.Start()
    WebBrowser1.Navigate(TextBox1.Text)
    Timer1.Start()

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If (c = 10) Then
    Timer1.Stop()
    End If
    End Sub

    Is there a wait command? How do I start the next url navigation after the timer period ends.

  4. #4
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Navigate to url and wait.

    For multiple webpages you could navigate to the next site at the end of the Timer's Tick Event.

    Here is an example that may help:
    VB.NET Code:
    1. Public Class Form1
    2.  
    3.     'A global list that you can fill with links that can be used within the Timers Tick Event
    4.     Private Links As List(Of String) = Nothing
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         'Add links into the List
    8.         Links = New List(Of String)(New String() {"http://www.google.com/", "http://www.yahoo.com/", "http://www.vbforums.com/"})
    9.  
    10.         'Navigate to the first link and start the timer
    11.         WebBrowser1.Navigate(New Uri(Links.First))
    12.         'Saving each Link in the Tag will help keep track of the current URL in the list (just in case the WebBrowser address changes due to a redirect etc).
    13.         WebBrowser1.Tag = Links.First
    14.         Timer1.Start()
    15.     End Sub
    16.  
    17.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    18.         'On each Tick of the Timer perform your logic
    19.  
    20.         '
    21.         'Your code here
    22.         '
    23.  
    24.         'Then check if all the links have been navigated
    25.         If Links.Last = WebBrowser1.Tag Then
    26.             'If the last link was reached then stop the timer
    27.             Timer1.Stop()
    28.             Links = Nothing
    29.             MessageBox.Show("Finished")
    30.         Else
    31.             'Otherwise navigate to the next link
    32.             Dim NextLink As String = Links(Links.IndexOf(WebBrowser1.Tag) + 1)
    33.             WebBrowser1.Navigate(New Uri(NextLink))
    34.             WebBrowser1.Tag = NextLink
    35.         End If
    36.     End Sub
    37.  
    38. End Class
    Last edited by JayJayson; Oct 17th, 2011 at 07:47 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: Navigate to url and wait.

    Thank you jay20aiii for sharing your elaborate code. There are some parts of it that is beyond my understanding but I believe I understand the structure.

    I don't believe it will do I would like to do which is navigate to a url and wait 30 seconds for the page to load, fill in some input textbox and then navigate to another url and do the same.

    From my understanding, your code will not wait 30 seconds to load the page before I add my code. I do not want to use the webbrowser document complete. I'm trying to implement a 30 second pause, delay or wait comment.

  6. #6
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Navigate to url and wait.

    The code example should do what you need. There are 3 things that would need to be 'tweaked' to make it work for you:
    1.
    VB.NET Code:
    1. Links = New List(Of String)(New String() {"http://www.google.com/", "http://www.yahoo.com/", "http://www.vbforums.com/"})
    Would be populated by your TextBoxes e.g.
    VB.NET Code:
    1. Links = New List(Of String)(New String() {TextBox1.Text, TextBox2.Text, TextBox3.Text})
    2. In the Designer after adding the Timer control to your form, you would need to set the Interval property to the relevant number of milliseconds. This would control how long the app waits before the Tick event runs. This is how you would make your code run 30 seconds after loading the URL.

    3. The code you want to run after 30 seconds would go in the Tick Event at the point I commented.

    The only other way I can think of to create a time delay, without freezing the WebBrowser control, would be to use:
    VB.NET Code:
    1. Application.DoEvents
    ... within a loop. The problem with that is it uses a lot of CPU. You might be able to combine the following in a loop to get the effect you need without eating too much CPU:
    VB.NET Code:
    1. Application.DoEvents
    2. System.Threading.Thread.Sleep(200)
    Last edited by JayJayson; Oct 18th, 2011 at 05:38 PM.

Tags for this Thread

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