Wait .5 seconds before continuing code VB.net
Asked Answered
L

12

31

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
Ludicrous answered 7/4, 2013 at 1:25 Comment(4)
How do you know that will be long enough?Psychotechnics
Because I've tested it. And that seems enough, but a when the java script function completes would be better.Ludicrous
What if my Internet connection is slower? Or my computer's JavaScript interpreter? My point is, assuming a particular time value is rarely a good solution. Certainly not a robust one.Psychotechnics
@koolboy5783: So you found an empirical evidence that this solution works for your case. It does not prove, however, that it will work in the long run. And, as Cody mentioned, it will most likely break soon after. If you are waiting on the event, you better indicate this in code - it will be easier to debug and troubleshoot as well.Floatage
Z
60

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
Zumstein answered 7/4, 2013 at 10:1 Comment(3)
Whenever I do that the program freezes first, and then it does the code.Ludicrous
I know. You have two options, either freeze the thread, or, use a timer (chipperyman573's suggestion)...Zumstein
I'm using on a VB.NET winform application and this does not cause my program to crash. I am waiting for a SQL update to finish but like @Neolisk mentions, event handling is probably the best way to go about this. Think I might need to create a question to address this since I do not see any posted on StackOverflow at the moment.Pelpel
A
18

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
Angele answered 1/4, 2016 at 17:36 Comment(4)
The value of second in wait function parameter will be in second or millicond?Cameleer
@SunnySandeep it's in seconds, because the if seconds = 2 we would loop 2 * 100 = 200 times and 200 * 10 (the value in Sleep(..)) = 2000 which in total is 2 seconds in milliseconds.Autarky
Timers.Timer is good when there aren't many instances of Timers.Timer. Also, it is easy to implement. But if there is a need to use multiple Timers.Timer, the execution slows down. For such situations, Thread.Wait() (Little complicated to implement) is a good idea. I say this with experience.Laflamme
this was gold for my current debug, thanks!Bibby
H
5
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

Howlett answered 18/11, 2017 at 19:18 Comment(0)
A
4

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
Ambsace answered 7/4, 2013 at 2:0 Comment(0)
D
0
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
Delta answered 3/4, 2018 at 8:15 Comment(0)
B
0

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
Bred answered 8/9, 2018 at 19:46 Comment(0)
S
0

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.

Stipe answered 28/4, 2019 at 19:4 Comment(0)
T
0

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.

Thermionics answered 2/7, 2019 at 10:29 Comment(1)
This works much better than the sleep() approach, which has way to much lag. For a 2 second delay in process time, this method works immediately, where 2 seconds using the sleep approach resulted in about a 10 second delay. In addition, I have a timer set with 5000 milliseconds delay for another subroutine that has to fire every 5 seconds to check something, and with the delay, I think the 10 second delay was equal to the 2 seconds I used for the sleep-based approach times the 5 second (5000 msec) timer delay. I think they interacted behind the scenes and caused longer lag.Shirring
M
0

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
Mcknight answered 5/8, 2020 at 17:56 Comment(0)
H
0
Public Sub WaitFor(Secs As Integer)
    Dim StartTime As DateTime = TimeOfDay
    Dim EndTime As DateTime = DateAdd(DateInterval.Second, Secs, StartTime)
    Do While StartTime < EndTime
        StartTime = TimeOfDay
    Loop
End Sub

Of course, this only works for full seconds, not partial secs.

Helman answered 26/3, 2023 at 18:58 Comment(1)
Given the question was for 0.5 seconds - would have been better to suggest a solution that dealt with milliseconds not full seconds.Kenning
N
0

Threading.Thread.Sleep(5000) and import Threading the wait for 5 seconds.

Natashianatassia answered 16/5, 2023 at 12:16 Comment(0)
M
-2

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

Metternich answered 24/6, 2014 at 15:52 Comment(1)
He asked for 0.5 seconds so it should be .Sleep(500) :)Autarky

© 2022 - 2025 — McMap. All rights reserved.