Jump to content
Sign in to follow this  


Recommended Posts


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
        $returnVal = ( Execute( $eval ) = $expected )
        $diff = TimerDiff( $start )
        If $diff >= $timeout Then ExitLoop
        Sleep( 10 )         
    Until ( $returnVal = True )
    Return $returnVal

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)
    ConsoleWrite( "! Scientific Mode was not detected in time" & @CRLF)

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

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
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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.