views:

78

answers:

2

I would like to know if its possible to "WinWaitActive" for "WindowWithThisTitle" and "WindowWithThatTitle" at the same time. I'm executing a command and there could be a window telling me that the connection failed or a user/pass dialog coming up.

Is there another way doing it as this?

WinWaitActive("Title1", "", 5)
If(WinExists("Title1")) Then
 MsgBox(0, "", "Do something")
Else
 If(WinExists("Title2")) Then
  MsgBox(0, "", "Do something else")
 EndIf
EndIf

Because I don't want to have the timeout which could be more than 15 seconds.

Thanks in advance!

+3  A: 

How about something like this.

$stillLooking = True
While $stillLooking
    $activeWindowTitle = WinGetTitle(WinActive(""))
    If $activeWindowTitle == "Title1" Then
        MsgBox(0, "", "Do something")
        $stillLooking = False
    ElseIf $activeWindowTitle == "Title2" Then
        MsgBox(0, "", "Do something else")
        $stillLooking = False
    EndIf
    sleep(5)
WEnd

Because I don't want to have the timeout which could be more than 15 seconds.

WinWaitActive() doesn't have a timeout unless you specify one. You gave it a five second timeout but you could leave that off and it would wait forever.

Copas
But this WhileLoop causes one cpu thread/core to run at 100%.I was thinking about a solution with WinWaitActive(regex) but I dont know how to create a regex that has an OR operator. Any Idea?What do you mean with "Don't forget to vote!" btw?
MemphiZ
The sleep(5) - or more - will fix the cpu problem, it was hard looping. WinWaitActive() wont do multiple targets and its return is a simple success bool so you can't really regex it. You have voted once since you started using stack overflow. The StackOverflow system really only works if people vote. If a question or answer is helpful to you you can vote it up with the up arrow picture or down with the down arrow. Good luck, hope this helped if it did you could show that by voting.
Copas
I didn't yet mark your answer as "THE" answer because maybe someone knows a method without looping. I'll just wait some time and then set it as answer if no one can provide a better way. Thanks for your help!
MemphiZ
WinWaitActive() also loops in the code of that function. I have a hard time imagining any way to do anything like this without looping in a procedural (vs. event driven) context. I'm sure a better way to do this exists but that way is also likely to have a loop in it.
Copas
I agree, being a procedural environment, this would be nearly impossible to accomplish without looping. I typically have my long running AutoIt loops sleep for an 1/8th of a second: Sleep(125) This polls often enough for most needs, and doesn't hog the CPU cycles.
JohnForDummies
I agree John, I usually do 200 or more in real world applications. A window typically will not come into focus and then leave focus in a time shorter then that. I just wanted to show that any sleep even a short one will solve it. Thanks for the +1!
Copas
A: 

You can use this Functions for two 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 Return 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

for more with examples

Ahmad Alshaikh