Sign in to follow this  
Followers 0
yoinkster

Can you do like "When %window% exists"

18 posts in this topic

Hey guys,

Another stupid question for you all! Hopefully I can make sense explaining it ...

Basically I've got my program which goes through a database update process with a 3rd party application and it does everything fine apart from potential error catching.

When an update is successfully completed a new child window comes up and at the minute all I do is sleep for a given time, test if the window exists and continue if it does and if not, it sleeps again for a bit.

What I'd like to be able to say is something like

"When %success window% exists

do command1

command2 etc

When %fail window% exists

do command3

command4 etc"

which is easy enough with an If instead of When as you can sleep and then check but I'd like to be able to do it without sleeping, can you like catch a window as it pops up and identify it somehow?

Share this post


Link to post
Share on other sites



will that catch it as soon as the window exists?

How would you put that into a custom function?

If you had something like

Func doUpdate

; pre update commands

While WinExists("success")
; Do code
Wend

While WinExists("fail")
; Do code
Wend

; post update commands
EndFuncoÝ÷ Û
.Ø­Á¨­º{bÞ¡ûayhbíý²mç§r©ë^têð¢é]ÖÞÂí¯x,¢g­)àÞ)jëh×6Func preUpdate

;pre update commands
doUpdate()
EndFunc

Func doUpdate
While WinExists("success")
someBool = y
; Do code
Wend
While WinExists("fail")
someBool = y
; Do code
Wend
postUpdate()
EndFunc

Func postUpdate
if someBool then
; post update  commands
else doUpdate()
EndFunc

Share this post


Link to post
Share on other sites

winwait will just pause the script till the window exists.. he wants to do something if the windows doesnt exist as well.. so if it never exists then the script will pause infinitely.. maybe putting a timeout will resolve this..

What say Brett?


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

if WinWait("success","",3) Then
;do something
Else
;do something
EndIf


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

That then gives rise to a static waiting time, sometimes the updates on the small databases will take 30seconds, others can take 5minutes.

And clients with rubbish workstations might even take longer than that!

So I'd like to get away from having a fixed waiting time if at all possible

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hmm, ok, how would that work?

Does it pause the script while the adlib function is called? How do you tell it to stop adlibing to the function? :)

Edited by yoinkster

Share this post


Link to post
Share on other sites

Just use WinWait(), the time is optional.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Just use WinWait(), the time is optional.

that would be risky.. what if the window doesn't turn up.. this will pause the script forever.. adlib funcs are a good idea..


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

that would be risky.. what if the window doesn't turn up.. this will pause the script forever.. adlib funcs are a good idea..

Then use the time functions .... it doesn't make any sense what so ever to use adlib functions when WinWait has a built in fall out already.

Edit for pseudo code:

If Not WinWait($h_wnd, $s_text, $i_max_timeout) Then
    MsgBox(16, "Error", "It failed")
Else
    MsgBox(64, "Info", "Success")
EndIf
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

I still don't get it :) :)

you do

func blah()
adlibenable("dave()", 1000)
adlibdisable
endfunc

func dave()
; some commands
endfunc

would then do ... what ? it enables the adlibbing of dave but then disables it in the next line, do you need to put something in dave() to tell blah() to go on to the next line of the function?

Share this post


Link to post
Share on other sites

In the end I did this, do you think it's a tidy enough solution?

While 1
    If %success% Then
        ; do stuffs
        ExitLoop
                ElseIf %fail% Then
        ; do other stuffs
        ExitLoop
    EndIf
WEnd

Share this post


Link to post
Share on other sites

oh thats just my notation sorry, in an attempt to differentiate between a variable ($), a program/function etc, it's just a label to indicate a psuedo result

Share this post


Link to post
Share on other sites

You are trying to wait until either a success or a fail window shows up?

Dim $windows[] = ["success", "fail"]

$window = _WinsWait($windows)

Switch $window
    Case -1
        MsgBox(0, "", "Timeout occured")
        ; This won't happen if you don't use the timeout
    Case 0
        MsgBox(0, "", "Success window found")
    Case 1
        MsgBox(0, "", "Fail window found")
    Case Else
        MsgBox(0, "", "Something went wrong")
EndSwitch

; Returns the index of the first window found or -1 on timeout
Func _WinsWait($arrayofwindows, $timeoutinmilliseconds = 0)
    Local $a
    If $timeoutinmilliseconds > 0 Then
        $timer = TimerInit()
        While TimerDiff($timer) < $timeoutinmilliseconds
            For $a = 0 To UBound($arrayofwindows) - 1
                If WinExists($arrayofwindows[$a]) Then Return $a
            Next
            Sleep(100)
        WEnd
        Return -1
    Else
        While True
            For $a = 0 To UBound($arrayofwindows) - 1
                If WinExists($arrayofwindows[$a]) Then Return $a
            Next
            Sleep(100)
        WEnd
    EndIf
EndFunc

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