코드 VB.net 을 계속하기 전에 0.5초 기다립니다.
코드가 있는데 중간 어딘가에서 기다렸다가 진행했으면 합니다.WebBrowser1 이후.문서.창. 돔 창.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
시스템을 사용해야 합니다.스레드화.스레드.sleep(밀리초)입니다.
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
이 질문은 오래되었지만 다른 사람들에게 유용하기 때문에 다음과 같은 대답이 있습니다.
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
위의 기능은 소프트웨어를 동결하지 않고 특정 시간 동안 대기하지만 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
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 이상의 높은 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 사용량 + 높은 지연
원하는 코드가 똑딱거릴 때 활성화되는 타이머를 만듭니다.타이머 코드의 첫 번째 줄이 다음인지 확인합니다.
timer.enabled = false
타이머를 타이머에 지정한 것으로 바꿉니다.
그런 다음 코드에 다음을 사용합니다.
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
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
다음 단계로 진행하기 전에 브라우저 준비 상태를 확인하여 더 나은 결과를 얻었습니다.브라우저가 "완료" 준비 상태가 될 때까지 아무것도 수행되지 않습니다.
Do While WebBrowser1.ReadyState <> 4
''' put anything here.
Loop
문제는Threading.Thread.SLeep(2000)
내 VB에서 먼저 실행된다는 것입니다.넷 프로그램.이것.
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
흠잡을 데 없이 일하는
제안된 코드에 결함이 있습니다.
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()는 자정 이후의 초를 반환합니다.자정 직전에 전화를 걸면 쉬는 시간이 거의 꼬박 하루가 될 것입니다.다음을 제안합니다.
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
DoEvents를 호출하면 루프 외부에서 수행할 수 있으므로 대기 루프를 취소해야 할 때 BreakCondition을 true로 설정할 수 있습니다.
또 다른 방법은 시스템을 사용하는 것입니다.스레드화.수동 재설정 이벤트
dim SecondsToWait as integer = 5
Dim Waiter As New ManualResetEvent(False)
Waiter.WaitOne(SecondsToWait * 1000) 'to get it into milliseconds
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
물론, 이것은 부분적인 초가 아니라 전체 초 동안만 작동합니다.
VB.net 4.0 프레임워크 코드:
Threading.Thread.Sleep(5000)
정수(밀리초)(1초 = 1000밀리초)
제가 테스트를 해봤는데 작동합니다.
언급URL : https://stackoverflow.com/questions/15857893/wait-5-seconds-before-continuing-code-vb-net
'sourcecode' 카테고리의 다른 글
보기가 창 계층 구조에 없는 UIViewController를 UIViewController에 표시하려고 합니다. (0) | 2023.05.14 |
---|---|
WPF 응용 프로그램 전체 화면 만들기(표지 시작 메뉴) (0) | 2023.05.14 |
SQL Server로 업데이트하려면 선택 (0) | 2023.05.14 |
npm 설치를 위한 --save 옵션은 무엇입니까? (0) | 2023.05.14 |
MongoDB: 하위 문서 업데이트 중 (0) | 2023.05.14 |