Jump to content
Sign in to follow this  
zfisherdrums

WaitForCondition

Recommended Posts

zfisherdrums

I've seen many requests on the forum on how to have a script wait until a particular condition exists. I've seen a common solution emerge in the responses. So I wrapped one in a function and called it WaitForCondition ( a not-too-subtle wink to Selenium ). It is not a new or even original implementation, but I've found it helpful.

;===============================================================================
;
; Function Name:    WaitForCondition( )
; Description::     Waits for the specified condition to evaluate to the expected result within a certain time
; Parameter(s):     $eval = Statement to evaluate ( shares 'Execute' limitations )
;                   $expected = Expected result
;                   $timeout = Time to wait ( in milliseconds ); Default = 5000
; Return Value(s):  True on Success; False on Failure
;
;===============================================================================
;
Func WaitForCondition( $eval, $expected, $timeout = 5000 )
    Local $start = TimerInit()
    Local $returnVal = False
    Do
        $returnVal = ( Execute( $eval ) = $expected )
        $diff = TimerDiff( $start )
        If $diff >= $timeout Then ExitLoop
        Sleep( 10 )         
    Until ( $returnVal = True )
    Return $returnVal
EndFunc

Here are some examples:

; ...WaitForCondition defined earlier...

; Run this script
; Open Calculator in Standard View
; Select View->Scientific View
If WaitForCondition( "ControlCommand( 'Calculator', '', 'Grads', 'IsVisible' )", 1, 30000 ) then 
    ConsoleWrite( "Scientific Mode Found!" & @CRLF)
Else
    ConsoleWrite( "! Scientific Mode was not detected in time" & @CRLF)
EndIf

; Now Switch to Standard Mode
If WaitForCondition( "ControlCommand( 'Calculator', '', 'Grads', 'IsVisible' )", 0, 30000 ) then 
    ConsoleWrite( "Standard Mode Found!" & @CRLF)
Else
    ConsoleWrite( "! Standard Mode was not detected in time" & @CRLF)
EndIf

And this one works nicely with the new beta ( now that limitations are removed from Execute ):

; User must enter the correct password within 30 seconds
If Not WaitForCondition( 'InputBox( "Authentication", "Please provide a password", "", "*" )', "test", 30000 ) then 
    MsgBox( 48, "Authentication Failure", "You are not allowed in!" )
    Exit 1
EndIf
Edited by zfisherdrums

Share this post


Link to post
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
Sign in to follow this  

×