Question

Ok, so I'm creating an AutoIt GUI executable to automate some functions in a website. However, I cannot find a way to break the While loop that executes the function I've created using a "STOP" button I've created. Therefore, it runs indefinitely, and I cannot quit the program. In theory, it should work, but I feel I'm missing something. Any help, folks?

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
Global $msg, $instructions, $AutoLevel, $NumDogsBox, $NumDogs, $IE, $i, $o

$IE = _IECreateEmbedded()
GUICreate("Automatic Dog Feeder for Criminal Case", 900, 690)

$instructions = GUICtrlCreateButton('     Instructions      ', 725, 5)
GUICtrlCreateLabel("# of Dogs: ",740,40)
$NumDogsBox = GUICtrlCreateInput("", 815, 37, 25)
$AutoLevel = GUICtrlCreateButton('  Begin Auto Feed Dog  ', 700, 65)

GUICtrlCreateObj($IE, 1, 100, 915, 610)

GUISetState(@SW_SHOW)

_IENavigate($IE, "http://apps.facebook.com/criminalcase")
$IE.Document.Body.Scroll = "no"
_IEAction($IE, "stop")

While 1
$msg = GUIGetMsg()
Select
  Case $msg = $GUI_EVENT_CLOSE
   ExitLoop
  Case $msg = $instructions
   MsgBox(0,"Instructions","1.) Log in to Facebook (if not already) in the window below" & @CRLF & "2.) Clear any notifications and get to the map screen" & @CRLF & "3.) Click on the dog bowl icon on the right of the map" & @CRLF & "4.) Enter the number of dogs you have to level up" & @CRLF & "5.) Click the Begin Auto Feed Dog button at the bottom of the window")
  Case $msg = $AutoLevel
      $NumDogs = GUICtrlRead($NumDogsBox)
      AutoDogLevel()
EndSelect
WEnd

Func AutoDogLevel()
    While 1
        $stop = GUICtrlCreateButton('STOP',830,65)
        $i=0
        While $i < $NumDogs
            If $msg = $stop Then
                ExitLoop
            Endif
            MsgBox(0,"","1xp" & $i+1)
            ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,156,651)
            Sleep(2000)
            If $msg = $stop Then
                ExitLoop
            Endif
            ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,723,524)
            MsgBox(0,"","Next Dog")
            Sleep(2000)
            $i = $i+1
        WEnd
    If $msg = $stop Then
        ExitLoop
    Endif
    While $i > 0
        If $msg = $stop Then
            ExitLoop
        Endif
        ControlClick("[CLASS:MacromediaFlashPlayerActiveX]","","","left",1,62,525)
        MsgBox(0,"","Previous Dog")
        If $msg = $stop Then
            ExitLoop
        Endif
        Sleep(1000)
        $i=$i-1
    WEnd
    If $msg = $stop Then
        ExitLoop
    Endif
    Sleep(3000)
WEnd
EndFunc

So, now I'm stuck. Any help will be greatly appreciated.

Was it helpful?

Solution

It's not a good idea to have more than 1 message loop for a GUI. Instead of doing it like this, I suggest keeping a variable for the current state, and only implementing a single loop. You also have some very large sleeps inside the message loop as well.

I haven't tested the code, but here is the message loop re-written to use a state variable and timers instead of multiple loops and sleeps. This is a much better design that will ensure your GUI is always responsive to ALL the buttons, and I think you'll find it easier to work with as well once you get used to the structure.

Local $iState = 0, $i, $iTimer
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $instructions
            MsgBox(0, "Instructions", "1.) Log in to Facebook (if not already) in the window below" & @CRLF & "2.) Clear any notifications and get to the map screen" & @CRLF & "3.) Click on the dog bowl icon on the right of the map" & @CRLF & "4.) Enter the number of dogs you have to level up" & @CRLF & "5.) Click the Begin Auto Feed Dog button at the bottom of the window")
        Case $msg = $AutoLevel
            $NumDogs = GUICtrlRead($NumDogsBox)
            $stop = GUICtrlCreateButton('STOP', 830, 65)

            $iState = 1
            $i = 0
        Case Else
            If $iState = 1 Then
                If $i >= $NumDogs Then
                    $iState = 3
                    ContinueLoop
                EndIf

                MsgBox(0, "", "1xp" & $i + 1)
                ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 156, 651)
                $iState = 2
                $iTimer = TimerInit()
            ElseIf $iState = 2 Then
                If TimerDiff($iTimer) < 2000 Then ContinueLoop

                ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 723, 524)
                MsgBox(0, "", "Next Dog")
                $iTimer = TimerInit()
                $i = $i + 1
            ElseIf $iState = 3 Then
                If TimerDiff($iTimer) < 1000 Then ContinueLoop

                If $i <= 0 Then
                    $iState = 1
                    ContinueLoop
                EndIf

                ControlClick("[CLASS:MacromediaFlashPlayerActiveX]", "", "", "left", 1, 62, 525)
                MsgBox(0, "", "Previous Dog")

                $iTimer = TimerInit()
                $i = $i - 1
            EndIf
    EndSelect
WEnd
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top