[vb.net] Wait .5 seconds before continuing code VB.net

I have a code and I want it to wait somewhere in the middle before going forward. After the WebBrowser1.Document.Window.DomWindow.execscript("checkPasswordConfirm();","JavaScript") I want it to wait .5 seconds and then do the rest of the code.

    WebBrowser1.Document.Window.DomWindow.execscript("checkPasswordConfirm();","JavaScript")

    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
    For Each webpageelement As HtmlElement In allelements
        If webpageelement.InnerText = "Sign Up" Then
            webpageelement.InvokeMember("click")
        End If
    Next

This question is related to vb.net wait

The answer is


I've had better results by checking the browsers readystate before continuing to the next step. This will do nothing until the browser is has a "complete" readystate

Do While WebBrowser1.ReadyState <> 4
''' put anything here. 
Loop

The problem with Threading.Thread.SLeep(2000) is that it executes first in my VB.Net program. This

Imports VB = Microsoft.VisualBasic

Public Sub wait(ByVal seconds As Single)
    Static start As Single
    start = VB.Timer()
    Do While VB.Timer() < start + seconds
        System.Windows.Forms.Application.DoEvents()
    Loop
End Sub

worked flawlessly.


Static tStart As Single, tEnd As Single, myInterval As Integer
myInterval = 5 ' seconds
tStart = VB.Timer()
tEnd = myInterval + VB.Timer()
Do While tEnd > tStart
    Application.DoEvents()
    tStart = VB.Timer()
Loop

This question is old but here is another answer because it is useful fo others:

thread.sleep is not a good method for waiting, because usually it freezes the software until finishing its time, this function is better:

   Imports VB = Microsoft.VisualBasic

   Public Sub wait(ByVal seconds As Single)
     Static start As Single
     start = VB.Timer()
     Do While VB.Timer() < start + seconds
       System.Windows.Forms.Application.DoEvents()
     Loop
   End Sub

The above function waits for a specific time without freezing the software, however increases the CPU usage.

This function not only doesn't freeze the software, but also doesn't increase the CPU usage:

   Private Sub wait(ByVal seconds As Integer)
     For i As Integer = 0 To seconds * 100
       System.Threading.Thread.Sleep(10)
       Application.DoEvents()
     Next
   End Sub

In web application a timer will be the best approach.

Just fyi, in desktop application I use this instead, inside an async method.

... 
Await Task.Run(Sub()
    System.Threading.Thread.Sleep(5000)
End Sub)
...

It work for me, importantly it doesn't freeze entire screen. But again this is on desktop, i try in web application it does freeze.


Imports VB = Microsoft.VisualBasic

Public Sub wait(ByVal seconds As Single)
    Static start As Single
    start = VB.Timer()
    Do While VB.Timer() < start + seconds
        System.Windows.Forms.Application.DoEvents()
    Loop
End Sub

%20+ high cpu usage + no lag

Private Sub wait(ByVal seconds As Integer)
    For i As Integer = 0 To seconds * 100
        System.Threading.Thread.Sleep(10)
        Application.DoEvents()
    Next
End Sub

%0.1 cpu usage + high lag


The suggested Code is flawed:

Imports VB = Microsoft.VisualBasic

Public Sub wait(ByVal seconds As Single)
    Static start As Single
    start = VB.Timer()
    Do While VB.Timer() < start + seconds
        System.Windows.Forms.Application.DoEvents()
    Loop
End Sub

VB.Timer() returns the seconds since midnight. If this is called just before midnight the break will be nearly a full day. I would suggest the following:

Private Sub Wait(ByVal Seconds As Double, Optional ByRef BreakCondition As Boolean = False)
    Dim l_WaitUntil As Date
    l_WaitUntil = Now.AddSeconds(Seconds)
    Do Until Now > l_WaitUntil
        If BreakCondition Then Exit Do
        DoEvents()
    Loop
End Sub

BreakCondition can be set to true when the waitloop should be cancelled as DoEvents is called this can be done from outside the loop.


You'll need to use System.Threading.Thread.Sleep(number of milliseconds).

WebBrowser1.Document.Window.DomWindow.execscript("checkPasswordConfirm();","JavaScript")

Threading.Thread.Sleep(500) ' 500 milliseconds = 0.5 seconds

Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
    If webpageelement.InnerText = "Sign Up" Then
        webpageelement.InvokeMember("click")
    End If
Next

VB.net 4.0 framework Code :

Threading.Thread.Sleep(5000)

The integer is in miliseconds ( 1 sec = 1000 miliseconds)

I did test it and it works


Make a timer, that activates whatever code you want to when it ticks. Make sure the first line in the timer's code is:

timer.enabled = false

Replace timer with whatever you named your timer.

Then use this in your code:

   WebBrowser1.Document.Window.DomWindow.execscript("checkPasswordConfirm();","JavaScript")
timer.enabled = true
Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
For Each webpageelement As HtmlElement In allelements
    If webpageelement.InnerText = "Sign Up" Then
        webpageelement.InvokeMember("click")
    End If
Next

Another way is to use System.Threading.ManualResetEvent

dim SecondsToWait as integer = 5
Dim Waiter As New ManualResetEvent(False)
Waiter.WaitOne(SecondsToWait * 1000) 'to get it into milliseconds