在继续代码 VB.net 之前等待 0.5 秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15857893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:48:19  来源:igfitidea点击:

Wait .5 seconds before continuing code VB.net

vb.netwait

提问by koolboy5783

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") 之后,我希望它等待 0.5 秒,然后执行其余代码。

    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

回答by Sam

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

您需要使用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

回答by Ali

I think this question is old but I provide another answer because it is useful fo others:

我认为这个问题很旧,但我提供了另一个答案,因为它对其他人有用:

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

thread.sleep不是一个好的等待方法,因为通常它会冻结软件直到完成它的时间,这个函数更好:

   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

this function wait for specific time without freezing the software, however increases the CPU usage, but below function not only does not freeze the software, but also does not increase the CPU usage:

此功能等待特定时间而不冻结软件,但会增加 CPU 使用率,但以下功能不仅不会冻结软件,而且不会增加 CPU 使用率:

    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

回答by Jon

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

回答by ?mer ?zer

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

%20+ 高 CPU 使用率 + 无延迟

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

%0.1 CPU 使用率 + 高延迟

回答by ibrahim.alzein

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

回答by Ricardo Mendoza

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

回答by user870036

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

问题Threading.Thread.SLeep(2000)在于它首先在我的 VB.Net 程序中执行。这个

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.

完美无缺。

回答by FroschMeat

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:

VB.Timer() 返回自午夜以来的秒数。如果这在午夜之前被调用,则休息时间将接近一整天。我建议如下:

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.

当在调用 DoEvents 时应取消等待循环时,可以将 BreakCondition 设置为 true,这可以从循环外部完成。

回答by Pro

VB.net 4.0 framework Code :

VB.net 4.0 框架代码:

Threading.Thread.Sleep(5000)

Threading.Thread.Sleep(5000)

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

整数以毫秒为单位(1 秒 = 1000 毫秒)

I did test it and it works

我确实测试过它并且有效