Wait .5 seconds before continuing code VB.net

后端 未结 11 679
长发绾君心
长发绾君心 2021-02-05 04:25

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();\",\"JavaSc

相关标签:
11条回答
  • 2021-02-05 05:15

    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
    
    0 讨论(0)
  • 2021-02-05 05:16

    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
    
    0 讨论(0)
  • 2021-02-05 05:18

    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

    0 讨论(0)
  • 2021-02-05 05:19

    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
    
    0 讨论(0)
  • 2021-02-05 05:20

    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.

    0 讨论(0)
提交回复
热议问题