Jump to content
Sign in to follow this  
RichardAR

why does this function continue to loop?

Recommended Posts

RichardAR

The following function works however even when $f is false, the do until loop continues on. I am not sure if this is a bug with the Do/Until block or something I'm doing wrong. FYI, I have it set up so $f is always False, and isColorMatch is always True in the application I'm testing this on.

Func somefunc()
    $f = False
    Do
        MouseClick("left", 748, 176, 1, 5)
        Sleep(500)
        Do
            $tc = PixelGetColor(1013, 256, $Hwnd)
            $ps = PixelSearch(748, 176, 761, 266, 0xFFFF00, 0, 1, $Hwnd)
            $f = IsArray($ps)
            If Not $f Then
                Send("{DOWN 5}")
                Sleep(500)
            EndIf
        Until $f Or IsColorMatch($tc, 0xB5B7B9, 10)
        If $f Then
            ConsoleWrite("f = true" & @CRLF)
            MouseClick("left", $ps[0], $ps[1], 1, 5)
            Sleep(500)
            MouseClick("left", 793, 77, 1, 5)
            Sleep(3000)
            While IsSomeFunc()
                Sleep(3000)
            WEnd
            MouseClick("left", 825, 77, 1, 5)
            Sleep(20000)
        Else
            ConsoleWrite("f = false" & @CRLF)
        EndIf
    Until ($f == False)
EndFunc

Share this post


Link to post
Share on other sites
martin

The following function works however even when $f is false, the do until loop continues on. I am not sure if this is a bug with the Do/Until block or something I'm doing wrong. FYI, I have it set up so $f is always False, and isColorMatch is always True in the application I'm testing this on.

Func somefunc()
    $f = False
    Do
        MouseClick("left", 748, 176, 1, 5)
        Sleep(500)
        Do
            $tc = PixelGetColor(1013, 256, $Hwnd)
            $ps = PixelSearch(748, 176, 761, 266, 0xFFFF00, 0, 1, $Hwnd)
            $f = IsArray($ps)
            If Not $f Then
                Send("{DOWN 5}")
                Sleep(500)
            EndIf
        Until $f Or IsColorMatch($tc, 0xB5B7B9, 10)
        If $f Then
            ConsoleWrite("f = true" & @CRLF)
            MouseClick("left", $ps[0], $ps[1], 1, 5)
            Sleep(500)
            MouseClick("left", 793, 77, 1, 5)
            Sleep(3000)
            While IsSomeFunc()
                Sleep(3000)
            WEnd
            MouseClick("left", 825, 77, 1, 5)
            Sleep(20000)
        Else
            ConsoleWrite("f = false" & @CRLF)
        EndIf
    Until ($f == False)
EndFunc

I think it's just a question of understanding that the number 0 does not equate exactly to False.

If $ps is not an array then

$f = IsArray($ps)

results in $f = 0

0 is a number not a boolean variable, and == tests for the variable type as well as the contents.

so if $f has the value of zero then

$f = false; would be true

$f == False; would be false

You could end your Do/Until with

Until $f = False

OR

Until Not $f


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Melba23

RichardAR,

Although it is difficult to be sure from just a small snippet of a full script like this, it looks to me as if there is a problem with the logic flow of your code.

There seems to be no way to reset $f to False once it is found to be True in the Inner Loop. So if $f is set to True there (quite likely given that it is one of your tests for exit!) there is no way it will ever satisfy the exit condition for the Outer Loop, which is why you never fall out of the Loop.

Try resetting $f to False each time you enter your Inner Loop. That way you are giving your Outer Loop exit condition a chance to be met.

I would strongly recommend that you write down a logic flow diagram for this section of your code and make sure that it is indeed acting as you want it to. Having nested loops with similar (and multiple) exit conditions can lead to some confusion in what result exactly is causing the loop to exit, or not!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
martin

RichardAR,

Although it is difficult to be sure from just a small snippet of a full script like this, it looks to me as if there is a problem with the logic flow of your code.

There seems to be no way to reset $f to False once it is found to be True in the Inner Loop. So if $f is set to True there (quite likely given that it is one of your tests for exit!) there is no way it will ever satisfy the exit condition for the Outer Loop, which is why you never fall out of the Loop.

Try resetting $f to False each time you enter your Inner Loop. That way you are giving your Outer Loop exit condition a chance to be met.

I would strongly recommend that you write down a logic flow diagram for this section of your code and make sure that it is indeed acting as you want it to. Having nested loops with similar (and multiple) exit conditions can lead to some confusion in what result exactly is causing the loop to exit, or not!

M23

I don't think that is quite correct.

I assume that the OP has seen that $f is 'false' from his ConsoleWrite lines. This will happen if in the inner loop $tc is an array but $ps is not. Then $f will be 0 and as I said, it won't pass the test $f == False at the end.

Once the condition at the end of the outer loop fails the whole loop starts again so it will continue until $f is 0 (once the condition is corrected). So I think the only thing wrong with the code shown is the "==".


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
Melba23

martin,

15-0. >_<

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
martin

martin,

15-0. :(

M23

Maybe just net service >_<


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
RichardAR

The problem was the outer loop, not the inner one. There should not have been a difference between = and == in a boolean statement with autoit; I try to use == so there is no confusion. This is what I did to fix the problem and now it works perfectly. Thanks for every one's input.

Func SomeFunc()
    ;ProcessSetPriority("AutoIt3.exe", 3) ; this speeds things up considerably
    $f = True
    While $f
        Do
            $ps = PixelSearch(748, 176, 761, 266, 0xFFFF00, 0, 1, $Hwnd)
            $f = IsArray($ps)
            If Not $f Then
                $tc = PixelGetColor(1013, 260, $Hwnd)
                If IsColorMatch($tc, 0x404346, 40) Then ExitLoop
                MouseClick("left", 748, 176, 1, 5)
                Sleep(500)
                Send("{DOWN 5}") ; assume 5 items are in view
                Sleep(500)
            EndIf
        Until $f
        If $f Then
            MouseClick("left", $ps[0], $ps[1], 1, 5)
            Sleep(500)
            MouseClick("left", 793, 77, 1, 5)
            Sleep(3000)
            While IsSomeFunc()
                Sleep(3000)
            WEnd
            MouseClick("left", 825, 77, 1, 5)
            Sleep(20000)
        EndIf
    WEnd
    MsgBox(64, "Some Title", "OK")
EndFunc

Share this post


Link to post
Share on other sites
martin

The problem was the outer loop, not the inner one. There should not have been a difference between = and == in a boolean statement with autoit;

Does that mean you didn't read my post or that you don't believe what I said?


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

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  

×