Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait .5 seconds before continuing code VB.net

Tags:

vb.net

wait

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 
like image 895
koolboy5783 Avatar asked Apr 07 '13 01:04

koolboy5783


2 Answers

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 
like image 184
Sam Avatar answered Sep 20 '22 16:09

Sam


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 
like image 38
Ali Avatar answered Sep 18 '22 16:09

Ali