Jump to content

Wait


Recommended Posts

A large part in automation is timing related. You want to wait for a window to appear. For a control to become enabled. You don't want your script to hang so you specify some timeout with a generic failure behavior. AutoIt solves that with special methods: WinWait, WinWaitClose, WinWaitActive, WinWaitNotActive. You can use those or you implement your custom solution which way too quickly becomes verbose (example in spoiler tags below).

Without crippling the question by including an answer: How can we do this better?

 

Const $TIMEOUT_MS = 5000
Const $CHECKWAIT_MS = 100

; Waiting for something to be true with a timeout and error handler
$start = TimerInit()
Local $success = False
While TimerDiff($start) < $TIMEOUT_MS
    $success = MyCondition()
    If $success Then ExitLoop
    Sleep($CHECKWAIT_MS)
WEnd
If Not $success Then
    MyErrorHandler()
EndIf

Func MyCondition()
    Static $n = 0
    $n += 1
    If $n == 10 Then
        Return True
    Else
        Return False
    EndIf
EndFunc

Func MyErrorHandler()
    ConsoleWriteError("Something happened" & @CRLF)
    Exit 1
EndFunc

 

 

 

Link to comment
Share on other sites

I generally do mine a little different, similar too the following usually.

Const $TIMEOUT_MS = 5000
Const $CHECKWAIT_MS = 100

; Waiting for something to be true with a timeout and error handler
$start = TimerInit()
Local $success = False

While 3
    If MyCondition() Then
        ExitLoop
    EndIf
    If TimerDiff($start) >= $TIMEOUT_MS Then
        MyErrorHandler()
    EndIf

    Sleep($CHECKWAIT_MS)
WEnd

Func MyCondition()
    Static $n = 0
    $n += 1
    If $n == 10 Then
        Return True
    Else
        Return False
    EndIf
EndFunc   ;==>MyCondition

Func MyErrorHandler()
    ConsoleWrite("Something happened" & @CRLF)
    Exit 1
EndFunc   ;==>MyErrorHandler

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...