Link to home
Start Free TrialLog in
Avatar of ClaudeWalker
ClaudeWalker

asked on

Timer instead of sleep vb.net

I am trying to have a program wait before execution however Threading.sleep does not allow execution while it's waiting.  

I'm trying to set up a timer to pause execution but I'm unsure how to do it.

Thanks,
JOe K.
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                        ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                        Handles WebBrowser1.DocumentCompleted

        'MsgBox(cnt)
        Dim t As Timer
        t.Start() 'how can I use this to delay the subsequent 
'lines of code.  Lets say I want a 3 second delay to allow
'a page to fully load (including javascript which the event
'is not waiting for).
        Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText
        My.Computer.FileSystem.WriteAllText("C:\Quib\" & cnt & ".txt", MSDNpage, False)
        nextPage()
    End Sub

Open in new window

Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Try a method like this:

    Public Sub Delay(ByVal DelayInSeconds As Integer)
        Dim ts As TimeSpan
        Dim targetTime As DateTime = DateTime.Now.AddSeconds(DelayInSeconds)
        Do
            ts = targetTime.Subtract(DateTime.Now)
            Application.DoEvents()
            System.Threading.Thread.Sleep(50)
        Loop While (ts.TotalSeconds > 0)
    End Sub

Added to your code:

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                        ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                        Handles WebBrowser1.DocumentCompleted

        Delay(3)

        Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText
        My.Computer.FileSystem.WriteAllText("C:\Quib\" & cnt & ".txt", MSDNpage, False)
        nextPage()
    End Sub
Avatar of ClaudeWalker
ClaudeWalker

ASKER

I tried it but it's not working because the sleep is halting all execution.

I think timer is the only way to go on this.

Thanks for the suggestion though
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@idle_Mind:  I posted the entire method.  The event is on DocumentComplete meaning when the HTML loads the event is run.  However, it does not what for javascript to run.  When I want to delay a a second for the javascript to run so when I scrape the screen I have the applicable data.  I know a delay will work because when I add a msgbox the javascript loads if I wait a second and then renders properly (saved to .txt)

@VBClassicGuy:  I tried Idle_Mind's method with no sleep.  No joy.

@CodeCruiser:  I tried the readystate but again that does not account for Javascript.  The WebBrowser object thinks it's ready when the HTML is assembled.

Any ideas?  The msgbox workaround works but I don't want to hit a msgbox 70 to 100 times.

Thanks,
JOe K.

I think I could do it if I multi-threaded the application vs the browser object.  

Any ideas on how to do that?

I have posted my code below (I put it back into one recursive method).
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, _
                        ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                        Handles WebBrowser1.DocumentCompleted

        

        Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText
        My.Computer.FileSystem.WriteAllText("C:\Quib\" & cnt & ".txt", MSDNpage, False)
        cnt += 1

        If totalPages = 0 Then
            totalPages = getTotalPages()
        End If

        Dim endTime As Date = Now.AddSeconds(3)

        While (Now <= endTime)
            Application.DoEvents()
        End While

        If cnt <> totalPages Then
            WebBrowser1.Navigate("http://www.quibids.com/categories/Recently-Completed/" & cnt, False)
        End If
    End Sub

Open in new window

GOT IT!

I used 4 webbrowsers letting each one load and then the object would check the previous one on document load (1 -> 4,  2 -> 1, 3 -> 2, 4 -> 3...)
Thanks for the direction.

-JOe K.