Jump to content

RESOLVED: For ... Next, timeout flag, Return value question


Recommended Posts

Hi gang, been a while since last here and playing with au3. Just getting back into the swing of scripting and knocking off some rust. I've bumped into a snag I can't quite figure out.

Here's an example of the code I'm working with:

For $j = 1 To 2
    _Result($j)
    If _Result($j) == "TIMED OUT" Then ContinueLoop

    MsgBox(0, "OUTSIDE FUNC", "$j = " & $j & "     ---   The RESULT was " & _Result($j))

Next

MsgBox(0, "RETURN TEST", "DONE")

Func _Result($i)
    If $i == 1 Then
        $timeout = MsgBox(0, "INSIDE FUNC", "$i = " & $i, 2)
        If $timeout <> 0 Then Return "OK PRESSED"
        If $timeout == 0 Then Return "TIMED OUT"
    EndIf
EndFunc   ;==>_Result

Q1. Why is the "INSIDE FUNC" MsgBox repeated multiple times? I'm would think the line would execute once then continue on.

Q2. When the "INSIDE FUNC" MsgBox is allowed to timeout (after 2 seconds) why isn't the "TIMED OUT" return value displayed in the last "RETURN TEST" MsgBox?

The multiple execution of the "$timeout =" code behaves the same with WinWait(), WinWaitActive(), and MsgBox() commands when I use the optional timeout flag. I'm using the latest production au3 build (3.3.6.0) on WinXPro SP3.

Thanks in advance!

ss3

Edited by ssubirias3
Link to comment
Share on other sites

Hi gang, been a while since last here and playing with au3. Just getting back into the swing of scripting and knocking off some rust. I've bumped into a snag I can't quite figure out.

Here's an example of the code I'm working with:

For $j = 1 To 2
    _Result($j)
    If _Result($j) == "TIMED OUT" Then ContinueLoop

    MsgBox(0, "OUTSIDE FUNC", "$j = " & $j & "     ---   The RESULT was " & _Result($j))

Next

MsgBox(0, "RETURN TEST", "DONE")

Func _Result($i)
    If $i == 1 Then
        $timeout = MsgBox(0, "INSIDE FUNC", "$i = " & $i, 2)
        If $timeout <> 0 Then Return "OK PRESSED"
        If $timeout == 0 Then Return "TIMED OUT"
    EndIf
EndFunc   ;==>_Result

Q1. Why is the "INSIDE FUNC" MsgBox repeated multiple times? I'm would think the line would execute once then continue on.

Q2. When the "INSIDE FUNC" MsgBox is allowed to timeout (after 2 seconds) why isn't the "TIMED OUT" return value displayed in the last "RETURN TEST" MsgBox?

The multiple execution of the "$timeout =" code behaves the same with WinWait(), WinWaitActive(), and MsgBox() commands when I use the optional timeout flag. I'm using the latest production au3 build (3.3.6.0) on WinXPro SP3.

Thanks in advance!

ss3

Each time you reference _Result() (3 times) the function is going to be executed. Your are returning a value in the _Result() function, but not assigning it to a variable when the function is called.. Play with this...

For $j = 1 To 2
    $return = _Result($j)
    MsgBox(0, "OUTSIDE FUNC", "$j = " & $j & "     ---   The RESULT was " & $return)
Next

MsgBox(0, "RETURN TEST", "DONE")

Func _Result($i)
        $timeout = MsgBox(0, "INSIDE FUNC", "$i = " & $i, 2)
        Switch $timeout
            Case 1
                Return "OK PRESSED"
            Case 2
                Return "CANCEL PRESSED"
            Case -1
                Return "TIMED OUT"
        EndSwitch

EndFunc   ;==>_Result

Edit: If you're only going to have the 2 possible return values from _Result() then the Switch statement is unnecessary and your original If/Then's were fine, although a Msgbox() timeout returns a value of -1.

Edited by Spiff59
Link to comment
Share on other sites

For $j = 1 To 2
    $Result=_Result($j)
    If $Result == "TIMED OUT" Then  ContinueLoop
    MsgBox(0, "OUTSIDE FUNC", "$j = " & $j & "     ---   The RESULT was " & $Result)
Next

MsgBox(0, "RETURN TEST", "DONE")

Func _Result($i)
    If $i == 1 Then
        $timeout = MsgBox(0, "INSIDE FUNC", "$i = " & $i,2)
        If $timeout == 1 Then Return "OK PRESSED" ;ok = 1
        If $timeout == -1 Then Return "TIMED OUT" ;timeout = -1
    EndIf
    Return(0)
EndFunc   ;==>_Result

Link to comment
Share on other sites

Each time you reference _Result() (3 times) the function is going to be executed. .... but not assigning it to a variable when the function is called..

Spiff59 & Nurav - thank you both for your insight!! Assigning to a variable was the missing key. The -1 verse 0 value on optional timeout flag was a minor oversite on my part. My actual script uses WinWaitActive() but for the forum example elected to use MsgBox() and didn't change the failure value.

Here's what I landed on as a working "example" of what I wanted to accomplish (for forum benefit). Again thanks guys, really appreciate the help!!

Cheers!!

For $j = 1 To 5
    $Result = _Result($j)
    If ($Result == "TIMED OUT") OR ($Result == "OK PRESSED") Then
        MsgBox(0, "OUTSIDE FUNC", "$j = " & $j & "     ---   The RESULT was " & $Result)
        ContinueLoop
    EndIf
Next

MsgBox(0, "RETURN TEST", "DONE")

Func _Result($i)
    If $i == 3 Then
        $timeout = MsgBox(0, "INSIDE FUNC", "$i = " & $i, 3)
;~         $timeout = WinWaitActive("INSIDE FUNC", "$i = ", 3)

        If $timeout == 1 Then Return "OK PRESSED" ;ok = 1
        If $timeout == -1 Then Return "TIMED OUT" ;timeout = -1 with MsgBox()
;~      If $timeout == 0 Then Return "TIMED OUT" ;timeout = 0  with WinWait() / WinWaitActive()
    EndIf
    Return (0)
EndFunc   ;==>_Result
Link to comment
Share on other sites

Why are you doing a case sensitive string comparison all the time? That's just weird :(

Maybe you just need to brush up your memory a bit, please see "Language Reference - Operators" in the helpfile.

Link to comment
Share on other sites

Thank you dear Sir!! At first I wasn't sure what you were talking about, but now I'm crystal clear and better for it!! Thinking back to why/where I picked up the habit, I don't recall exactly. Think someone here explained the difference between "=" and "==" as being something like equal vs equal to. Meaning use "=" when setting a variable and use "==" when checking if the variable was "equal to" a value.

But the au3 Help file is quite clear that for proper au3 syntax the "==" should only be used for string comparisons that need to be case sensitive. Thanks for the nudge in the right direction. And to think had you not been kind enough to mention this, I would have continued this poor habit.

Long live the Admiral!!

Link to comment
Share on other sites

The "Language Reference - Operators" section of the helpfile is remiss regarding the difference between the "=" and "==" operators. The four variables defined in the example below all test as equal with "=", and none pass the test when "==" is used. So, the helpfile stating that case-sensitivity is the only difference is far from the truth. Different behavior is also exhibited when comparing booleans, the NUL character (ASCII 0), Character "0" (ASCII 48), and null or empty strings.

$w = False
$x = Chr(0) & Chr(0); 2-character string
$y = 0
$z = ""

If $x = $y then beep(500,1000)
Sleep(100)
If $x = $z then beep(500,1000)
Sleep(100)
If $y = $z then beep(500,1000)
Sleep(100)
If $w = $y then beep(500,1000)
Sleep(100)
If $w = $z then beep(500,1000)


If $x == $y then beep(1000,50)
Sleep(100)
If $x == $z then beep(1000,50)
Sleep(100)
If $y == $z then beep(1000,50)
Sleep(100)
If $w == $y then beep(1000,50)
Sleep(100)
If $w == $z then beep(1000,50)
Edited by Spiff59
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...