Jump to content

Function continues to return False


Recommended Posts

Hey everyone,

First post - not the best at AutoIt by any means but I have this function that does 2 things: checks to see if a window is open and if not display a "not open" message on a gui. The function is recursive and uses global variables to keep track of a "3 strikes you're out system" Context out of the way, here is a dumb version of the code:

Global $retrycount = 0

Func _CheckIfActive()
   ; This $gui is already created, not the issue
   Local $locgui = $gui
   
   ; What I will be returning
   Local $return = False
   
   ; Adding buttons to GUI
   Local $exit = GUICtrlCreateButton("Exit", 10, 470, 153, 40)
   
   ; Attempting to activate.
   ConsoleWrite("Attepting to Activate" & @CRLF)
   WinActivate("Lotus Notes")
   
   ; If still not active
   If NOT WinActive("Lotus Notes") Then
      ConsoleWrite("Not Active" & @CRLF)
      Local $activeError = GUICtrlCreateLabel("Lotus Notes is not open! Please make sure it is the active page.", 0, 410, 500, 20, $SS_CENTER) ;--> Error message I was referencing
      Local $retry = GUICtrlCreateButton("Retry?", 230, 430, 40, 20) ;--> Retry button
      While WinExists($title)
         Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE, $exit
            Exit ;--> Obviously
         Case $retry
            $retrycount += 1 ;--> Increase $retrycount by 1 every time it is pressed
            ConsoleWrite("Retry: " & $retrycount & @CRLF)
            If $retrycount > 3 Then ;--> 3 strikes you're out
               ConsoleWrite("Returning: False" & @CRLF)
               $retrycount = 0
               Return False ;--> Force return false because it can't find page
            EndIf
            ; Resetting messages and buttons
            GUICtrlDelete($activeError)
            GUICtrlDelete($retry)
            GUICtrlDelete($exit)
            ConsoleWrite("Checking Again." & @CRLF)
            _CheckIfActive() ;--> Recursive Code
            Return False ;--> Needed or it will break for some reason
         EndSwitch
      WEnd
   Else ;--> If it can find "Lotus Notes" do this:
      $return = True
      ConsoleWrite("Active" & @CRLF)
      GUICtrlDelete($exit) ;--> Resetting buttons
      Local $processing = GUICtrlCreateLabel("Connected to Lotus Notes Service. Running the program!", 0, 410, 500, 20, $SS_CENTER) ;--> Printing message
      ConsoleWrite("Returning: " & $return & @CRLF) ;--> Literally says Returning: True and will still yet return False
      Return $return
   EndIf
EndFunc

Func _RunPrnt()
   Local $active = _CheckIfActive()
   ConsoleWrite("A?: " & $active & @CRLF) ;--> This will print False when it should print True
   If $active = True Then
       ; Do stuff
   EndIf
EndFunc

I have no idea where the error is coming from.

Ideas?

Link to comment
Share on other sites

Hey can you try replacing

If NOT WinActive("Lotus Notes") Then

with

If WinActive("Lotus Notes") = 0 Then

Let be known what happens

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

1 minute ago, careca said:

Hey can you try replacing

If NOT WinActive("Lotus Notes") Then

with

If WinActive("Lotus Notes") = 0 Then

Let be known what happens

Gave it a spin, here is the output after testing the retry feature with it. (Based on the ConsoleWrite() function)

(WITH RETRY)

Attepting to Activate
Not Active
Retry: 1
Checking Again.
Attepting to Activate
Not Active
Retry: 2
Checking Again.
Attepting to Activate
Active
Returning: True
A?: False

It should also be mentioned that when this is run without needing to "retry" because it detected it immediately -- there is no issue:

(WITHOUT USING RETRY)

Attepting to Activate
Active
Returning: True
A?: True
Running Print

 

Link to comment
Share on other sites

Hi @NoEffort, and welcome to the AutoIt forums :welcome:

Since recursion is a very careful argument to deal with (and not really necessary in this situation), why don't you use something like this (untested)?

#include <MsgBoxConstants.au3>

Func _CheckWindowExistance($strWindowName)

    Local $intRetryCount = 0, _
          $intMaximumRetries = 3, _
          $blnWindowFound = False, _
          $intAnswer
          
    
    Do
       $blnWindowFound = WinActivate($strWindowName)
       If $blnWindowFound = False Then
           $intAnswer = MsgBox($MB_YESNO, "Window not found!", "You can try again " & $intMaximumRetries - $intRetryCount & " times." & @CRLF & "Retry?")
           If $intAnswer = $IDYES Then
               $intRetryCount += 1
               ContinueLoop
           Else
               ExitLoop
           EndIf
       Else
           ConsoleWrite("Window found after " & $intRetryCount & " retries!" & @CRLF)
           Return True
       EndIf             
    Until $intRetryCount = $intMaximumRetries Or $blnWindowFound = True

:)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

34 minutes ago, FrancescoDiMuro said:

Hi @NoEffort, and welcome to the AutoIt forums :welcome:

Since recursion is a very careful argument to deal with (and not really necessary in this situation), why don't you use something like this?

Func _CheckWindowExistance($strWindowName)

    Local $intRetryCount = 0, _
          $intMaximumRetries = 3, _
          $blnWindowFound = False, _
          $intAnswer
          
    
    Do
       $blnWindowFound = WinActivate($strWindowName)
       If $blnWindowFound = False Then
           $intAnswer = MsgBox($MB_YESNO, "Window not found!", "You can try again " & $intMaximumRetries - $intRetryCount & " times." & @CRLF & "Retry?")
           If $intAnswer = $IDYES Then
               $intRetryCount += 1
               ContinueLoop
           Else
               ExitLoop
           EndIf
       Else
           ConsoleWrite("Window found after " & $intRetryCount & " retries!" & @CRLF)
           Return True
       EndIf             
    Until $intRetryCount <= $intMaximumRetries Or $blnWindowFound = False

:)

In an attempt to implement this clever way of doing this (if I do say so myself), I realized that it was calling the recursive portion of the function before it returned a value. What I mean is, because of the While loop for the GUI and the requirement to click "Retry" to test again -- it would always return whatever was at the bottom of that case (Case $retry). Therefore, I implemented a global boolean variable that gets changed to a specific value before returning based on the situation. (When it triggers that the screen is active, it will change the boolean from False to True and return that).

Would not be surprised if there was a better way to do this -- and yours is certainly much more compact but I can live with it for now.

Thanks for helping me realize what was happening :)

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...