Jump to content

Can you do like "When %window% exists"


Recommended Posts

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?

Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
Share on other sites

  • Moderators

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.

Link to comment
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...