Sign in to follow this  
Followers 0
Ashalshaikh

2WinWait (Wait For 2 Windows)

4 posts in this topic

#1 ·  Posted (edited)

Hi

First :: I'm Sorry , My English Is Bad >_<

!!!

Just Small Idea !!

To Wait For 2 Windows ..

; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait
; Description ...: Wait For Tow Windows .
; Syntax.........: _2WinWait ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle  - Title Of First  Wondow 
;                  $SecondTitle - Title Of Second Wondow 
;                  $FirstText   - Text  Of First  Wondow 
;                  $SecondText  - Text  Of Second Wondow 
; Return values .: Success - None
;                  Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......: 
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
    If $FirstTitle = "" Or $SecondTitle = "" Then
        Return 0 
    Else
        Do 
        Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
    EndIf
EndFunc


; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait_Any 
; Description ...: Wait For Tow Windows And Select Any Window Id Exists .
; Syntax.........: _2WinWait_Any ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle  - Title Of First  Wondow 
;                  $SecondTitle - Title Of Second Wondow 
;                  $FirstText   - Text  Of First  Wondow 
;                  $SecondText  - Text  Of Second Wondow 
; Return values .: Success - Number Of Window ==> 1= First Window , 2= Second Window
;                  Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......: 
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait_Any ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
    If $FirstTitle = "" Or $SecondTitle = "" Then
        Return 0 
    Else
        Do 
        Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
        If WinExists ($FirstTitle,$FirstTexit) Then 
            Return 1 
        Else
            Return 2 
        EndIf
    EndIf
EndFunc

Thanks !!

Edited by Ashalshaikh

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Personally, if you're going to wait for start waiting for more than one window, we might as well develop something that can wait for 'n' windows instead of just 2 or just 3... etc.

Here, I just cranked this out:

(a Sleep() probably needs to be added somewhere unless we don't mind a wait loop going full-bore)

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinWaitExist_Array
; Description ...: Allows the equivalent of a multi-window WinWait
; Syntax ........: _WinWaitExist_Array(ByRef $aWindows[, $iMode=0[, $iTimeout=-1]])
; Parameters ....: $aWindows - A variable containing a 2-dimensional array of window matches in the following format:
;                  | [0][0] = number of windows in the array (ignored)
;                  | [1][0] = title or handle of window 1
;                  | [1][1] = text of window 1
;                  | ...
;                  | [n][0] = title or handle of window n
;                  | [n][1] = text of window n
;                  $iMode - integer specifying the function operating conditions:
;                  | 0 = Function continues to run until all windows in the array
;                         exist or the timeout is reached (WaitExists for Entire Array)
;                  | 1 = Function continues to run until at least one window in
;                          the array exists or the timeout is reached (WaitExists for Any Element)
;                  | 2 = Function continues to run until at least one window in
;                         the array doesn't exist or the timeout is reached (WaitClosed for Any Element)
;                  | 3 = Function continues to run until all windows in the array
;                         don't exist or the timeout is reached (WaitClosed for Entire Array)
;                  $iTimeout - specifies a timeout in miliseconds to wait for the 'iMode' conditions to be met before failing.
; Return values .: Success - Returns True
;                  | iMode==0 - @extended is set to the time in miliseconds it took to complete checking
;                  | iMode==1 - @extended is set to the index first existent window
;                  | iMode==2 - @extended is set to the index first non-existent window
;                  | iMode==3 - @extended is set to the time in miliseconds it took to complete checking
;                  Failure - Returns False and sets @error to:
;                  | 0 = Timeout was reached before the chosen conditions
;                  | 1 = The input array has an incorrect number of dimensions
;                  | 2 = The input array has an invalid number of elements
; Author ........: Crash Daemonicus (crashdemons)
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================

Func _WinWaitExist_Array(ByRef $aWindows,$iMode=0,$iTimeout=-1)
    If UBound($aWindows,0)<>2 Then Return SetError(1,0,False)
    Local $iLastWindow=UBound($aWindows)-1
    If $iLastWindow<1 Then Return SetError(2,0,False)
    If Not ($iMode=0 Or $iMode=1 Or $iMode=2 Or $iMode=3) Then Return SetError(3,0,False)
    Local $iTimer=TimerInit()
    While $iTimeout<0 Or TimerDiff($iTimer)<$iTimeout
        For $i=1 To $iLastWindow
            If WinExists($aWindows[$i][0],$aWindows[$i][1]) Then
                If $iMode=1 Then Return SetExtended($i,True); at least 1 window exists
                If $iMode=3 Then ContinueLoop 2
            Else
                If $iMode=2 Then Return SetExtended($i,True); at least 1 window doesnt exists
                If $iMode=0 Then ContinueLoop 2; restart loop so we can try again to check if all windows exist (could just start at $i...)
            EndIf
        Next
        If $iMode=0 Or $iMode=3 Then Return SetExtended(TimerDiff($iTimer),True)
    WEnd
    Return False
EndFunc

Examples:

  • Wait for an array of windows to exist:
;waits 5000ms max for both Windows Media Player and new Notepad windows to exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,0,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)
  • Wait for an array of windows to be closed (not exist):
;waits 5000ms max for both Windows Media Player and new Notepad windows to NOT exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,3,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)
  • Wait for any item of an array of windows to exist:
;waits 5000ms max for either Windows Media Player OR a new Notepad window to exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,1,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)

  • Wait for any item of an array of windows to be closed (not exist):
;waits 5000ms max for either Windows Media Player OR a new Notepad window to NOT exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,2,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)
Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

Personally, if you're going to wait for start waiting for more than one window, we might as well develop something that can wait for 'n' windows instead of just 2 or just 3... etc.

Here, I just cranked this out:

(a Sleep() probably needs to be added somewhere unless we don't mind a wait loop going full-bore)

; #FUNCTION# ====================================================================================================================
; Name ..........: _WinWaitExist_Array
; Description ...: Allows the equivalent of a multi-window WinWait
; Syntax ........: _WinWaitExist_Array(ByRef $aWindows[, $iMode=0[, $iTimeout=-1]])
; Parameters ....: $aWindows - A variable containing a 2-dimensional array of window matches in the following format:
;                  | [0][0] = number of windows in the array (ignored)
;                  | [1][0] = title or handle of window 1
;                  | [1][1] = text of window 1
;                  | ...
;                  | [n][0] = title or handle of window n
;                  | [n][1] = text of window n
;                  $iMode - integer specifying the function operating conditions:
;                  | 0 = Function continues to run until all windows in the array
;                         exist or the timeout is reached (WaitExists for Entire Array)
;                  | 1 = Function continues to run until at least one window in
;                          the array exists or the timeout is reached (WaitExists for Any Element)
;                  | 2 = Function continues to run until at least one window in
;                         the array doesn't exist or the timeout is reached (WaitClosed for Any Element)
;                  | 3 = Function continues to run until all windows in the array
;                         don't exist or the timeout is reached (WaitClosed for Entire Array)
;                  $iTimeout - specifies a timeout in miliseconds to wait for the 'iMode' conditions to be met before failing.
; Return values .: Success - Returns True
;                  | iMode==0 - @extended is set to the time in miliseconds it took to complete checking
;                  | iMode==1 - @extended is set to the index first existent window
;                  | iMode==2 - @extended is set to the index first non-existent window
;                  | iMode==3 - @extended is set to the time in miliseconds it took to complete checking
;                  Failure - Returns False and sets @error to:
;                  | 0 = Timeout was reached before the chosen conditions
;                  | 1 = The input array has an incorrect number of dimensions
;                  | 2 = The input array has an invalid number of elements
; Author ........: Crash Daemonicus (crashdemons)
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================

Func _WinWaitExist_Array(ByRef $aWindows,$iMode=0,$iTimeout=-1)
    If UBound($aWindows,0)<>2 Then Return SetError(1,0,False)
    Local $iLastWindow=UBound($aWindows)-1
    If $iLastWindow<1 Then Return SetError(2,0,False)
    If Not ($iMode=0 Or $iMode=1 Or $iMode=2 Or $iMode=3) Then Return SetError(3,0,False)
    Local $iTimer=TimerInit()
    While $iTimeout<0 Or TimerDiff($iTimer)<$iTimeout
        For $i=1 To $iLastWindow
            If WinExists($aWindows[$i][0],$aWindows[$i][1]) Then
                If $iMode=1 Then Return SetExtended($i,True); at least 1 window exists
                If $iMode=3 Then ContinueLoop 2
            Else
                If $iMode=2 Then Return SetExtended($i,True); at least 1 window doesnt exists
                If $iMode=0 Then ContinueLoop 2; restart loop so we can try again to check if all windows exist (could just start at $i...)
            EndIf
        Next
        If $iMode=0 Or $iMode=3 Then Return SetExtended(TimerDiff($iTimer),True)
    WEnd
    Return False
EndFunc

Examples:

  • Wait for an array of windows to exist:
;waits 5000ms max for both Windows Media Player and new Notepad windows to exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,0,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)
  • Wait for an array of windows to be closed (not exist):
;waits 5000ms max for both Windows Media Player and new Notepad windows to NOT exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,3,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)
  • Wait for any item of an array of windows to exist:
;waits 5000ms max for either Windows Media Player OR a new Notepad window to exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,1,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)

  • Wait for any item of an array of windows to be closed (not exist):
;waits 5000ms max for either Windows Media Player OR a new Notepad window to NOT exist
Dim $arr[3][2]= [[2,0],   ['Windows Media Player',''], ['Untitled - Notepad','']   ]
$ret=_WinWaitExist_Array($arr,2,5000)
MsgBox(0,'','Return value: '&$ret&@CRLF&'@error: '&@error&@CRLF&'@extended: '&@extended)

WOW !!! Smart Idea !! >_<

Thanks !!

Share this post


Link to post
Share on other sites

You should always be careful when it comes to more complex setups that MAY contain multiple windows with the same title...

1) Inside of the same process you may have to check for the correct control.

2) If there are several processes creating Windows with the same title, you have to check the ($title,$pid) tuple. This is what I have to deal with:

You can get

- the handle from the window (title), WinGetHandle or from WinList()

- the pid from the window (title), WinGetProcess

- but NOT the pid from a handle: for that you have to loop through winlist()

Func JM_Debug($text)
    ConsoleWrite($text & @CRLF)
EndFunc

Func JM_GetHwnd($title, $pid)
    ; find the handle belonging to the ($title,$pid)-tuple
    Local $Windows = WinList()
    Local $WinSearch = -1
    Local $WIN = -1
    Local $lpid = -1
    Local $hwnd = -1

    JM_Debug("[JM_GetHwnd] start: title>" & $title & "< pid>" & $pid & "<. Window count: >" & $Windows[0][0] & "<")
    For $WinSearch = 1 To $Windows[0][0]
        $WIN = $Windows[$WinSearch][0]
        $hwnd = $Windows[$WinSearch][1]
        $lpid = WinGetProcess($WIN)
        If ($WIN <> "") And BitAnd(WinGetState($Windows[$WinSearch][1]), 1) Then
            ;JM_Debug("[JM_GetHwnd] " & $WinSearch & "/" & $Windows[0][0] & " Considering >" & $WIN & "< of PID>" & $lpid & "< hwnd>" & $hwnd & "<")
            If (StringInStr($WIN, $title) And $lpid=$pid) Then
                Return $hwnd
            EndIf
        EndIf
    Next

    JM_Debug("[JM_GetHwnd] Return >" & $hwnd & "<")
    Return ""
EndFunc

Func _WinWaitExist_Array(ByRef $aWindows, $iMode=0, $iTimeout=-1)
    ; mode: 0: all windows must exist
    ; mode: 1: any window shall exist <--- most interesting for us
    ; mode: 2: any window shall NOT exist
    ; mode: 3: NO window shall exist
    ; aWindow[$i][0] = title
    ; aWindow[$i][1] = text
    ; aWindow[$i][2] = pid
    If UBound($aWindows, 0)<>2 Then Return SetError(1, 0, False) ; check number of dimensions
    Local $iLastWindow = UBound($aWindows)-1
    If $iLastWindow<1 Then Return SetError(2, 0, False)
    If Not ($iMode=0 Or $iMode=1 Or $iMode=2 Or $iMode=3) Then Return SetError(3, 0, False)
    Local $iTimer=TimerInit()
    While $iTimeout<0 Or TimerDiff($iTimer)<($iTimeout*1000)
        For $i=0 To $iLastWindow
            If WinExists($aWindows[$i][0], $aWindows[$i][1]) And JM_GetHwnd($aWindows[$i][0], $aWindows[$i][2]) Then
                If $iMode=1 Then Return SetExtended($i, True); at least 1 window exists
                If $iMode=3 Then ContinueLoop 2
            Else
                If $iMode=2 Then Return SetExtended($i, True); at least 1 window doesn't exist
                If $iMode=0 Then ContinueLoop 2; restart loop so we can try again to check if all windows exist (could just start at $i...)
            EndIf
        Next
        If $iMode=0 Or $iMode=3 Then Return SetExtended(TimerDiff($iTimer), True)
    WEnd
    Return SetError(99, 0, False)
EndFunc

Note the change "For $i=0" (before $i=1) otherwise you miss the first window!

Note also that I changed the function to use seconds instead of milliseconds, since the timeout parameter for almost every function except sleep() is in seconds.

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