Timer instead of sleep vb.net

ClaudeWalker
ClaudeWalker used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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

Author

Commented:
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
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
I'm only sleeping for 50 milliseconds, which is VERY short, and will allow it to continue processing messages when DoEvents() is hit but won't ping the processor to 100% usage.

I'm guessing something else is causing the problem...can you post more code?
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2009
Commented:
Try Idle_Mind's code without the sleep command...just loop around with doevents until the time comes to exit the do loop.
Most Valuable Expert 2012
Top Expert 2014
Commented:
Try this

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

     If WebBrowser1.ReadyState = WebBrowserReadyState.Completed Then
        Dim MSDNpage As String = WebBrowser1.Document.Body.InnerText
        My.Computer.FileSystem.WriteAllText("C:\Quib\" & cnt & ".txt", MSDNpage, False)
        nextPage()
     End If
   
End Sub

Author

Commented:
@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.

Most Valuable Expert 2012
Top Expert 2014

Commented:

Author

Commented:
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

Author

Commented:
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...)

Author

Commented:
Thanks for the direction.

-JOe K.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial