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

Visual Basic.NETVisual Basic Classic

Avatar of undefined
Last Comment
ClaudeWalker
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of VBClassicGuy
VBClassicGuy
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of ClaudeWalker
ClaudeWalker

ASKER

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

Avatar of ClaudeWalker
ClaudeWalker

ASKER

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

Avatar of ClaudeWalker
ClaudeWalker

ASKER

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...)
Avatar of ClaudeWalker
ClaudeWalker

ASKER

Thanks for the direction.

-JOe K.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo