Sign in to follow this  
Followers 0
durden

problems with while loop in GUI

9 posts in this topic

Hi,

I'm attempting to write a script that has a couple of buttons (start, stop) and a couple of edit fields. When the user clicks the start button, I need it to run some sort of loop until they click the stop button. My problem is that once that loop is started with the user hitting the start button, the main GUI loop cannot regain control because its obviously stuck in an endless loop. :P Whats the best way to handle this? Here is an example of the code:

#include <GUIConstants.au3>

GUICreate("Test", 300, 300)

GUICtrlCreateLabel("Left Turn Time", 20, 10)
$left = GUICtrlCreateEdit("", 20, 30, 150, 40)

GUICtrlCreateLabel("Right Turn Time", 20, 90)
$right = GUICtrlCreateEdit("", 20, 110, 150, 40)

$startButton = GUICtrlCreateButton("Start", 20, 270, 60)
$stopButton = GUICtrlCreateButton("Stop", 90, 270, 60)
GUISetState(@SW_SHOW)

While 1
  $msg = GUIGetMsg()

  Select
    Case $msg = $startButton
      MsgBox(0, "GUI Event", "You pressed start button!")
      
        While 1
            ;... blah ...
            Sleep(Random(120000,240000))
        WEnd
    Case $msg = $stopButton
      MsgBox(0, "GUI Event", "You pressed stop button!")

    Case $msg = $GUI_EVENT_CLOSE
      MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
      ExitLoop
  EndSelect
WEnd

Share this post


Link to post
Share on other sites



maybe...

#include <GUIConstants.au3>
 
 Dim $show, $hold

GUICreate("Test", 300, 300)

GUICtrlCreateLabel("Left Turn Time", 20, 10)
$left = GUICtrlCreateEdit("", 20, 30, 150, 40)

GUICtrlCreateLabel("Right Turn Time", 20, 90)
$right = GUICtrlCreateEdit("", 20, 110, 150, 40)

$startButton = GUICtrlCreateButton("Start", 20, 270, 60)
$stopButton = GUICtrlCreateButton("Stop", 90, 270, 60)
GUISetState(@SW_SHOW)

While 1
  $msg = GUIGetMsg()

  Select
    Case $msg = $startButton
      MsgBox(0, "GUI Event", "You pressed start button!")
      $show = 1
        
    Case $msg = $stopButton
      MsgBox(0, "GUI Event", "You pressed stop button!")
        $show = 0
    Case $msg = $GUI_EVENT_CLOSE
      MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
      ExitLoop
EndSelect

    If $show = 1 And $hold = 0 Then
        ToolTip(".... Valuater", 10, 10, "Thanks...", 1)
        $hold = 1
    ElseIf $show = 0 And $hold = 1 Then
        ToolTip("")
        $hold = 0
    EndIf

WEnd

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $startButton
            MsgBox(0, "GUI Event", "You pressed start button!")
            While 1
                $msg = GUIGetMsg()
                ;... blah ...
                If $msg = $stopButton Then
                    MsgBox(0, "GUI Event", "You pressed stop button!")
                    ExitLoop
                EndIf
            WEnd
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
            ExitLoop
    EndSelect
WEnd

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Try this

#include <GUIConstants.au3>

GUICreate("Test", 300, 300)
 
GUICtrlCreateLabel("Left Turn Time", 20, 10)
$left = GUICtrlCreateEdit("", 20, 30, 150, 40)

GUICtrlCreateLabel("Right Turn Time", 20, 90)
$right = GUICtrlCreateEdit("", 20, 110, 150, 40)

$startButton = GUICtrlCreateButton("Start", 20, 270, 60)
$stopButton = GUICtrlCreateButton("Stop", 90, 270, 60)
GUISetState(@SW_SHOW)

While 1
  $msg = GUIGetMsg()
  Select
    Case $msg = $startButton
      MsgBox(0, "GUI Event", "You pressed start button!")
        While 1
            $msg = GUIGetMsg()
            ToolTip("Now in the Startbutton loop",0,0)
            If $Msg = $StopButton then 
                MsgBox(0, "GUI Event", "You pressed stop button!")
                Exitloop            
            Endif
        WEnd
     Case $msg = $stopButton
      MsgBox(0, "GUI Event", "You pressed stop button! But Press Start First")

    Case $msg = $GUI_EVENT_CLOSE
      MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
      ExitLoop
  EndSelect
WEnd
Edited by Paulie

Share this post


Link to post
Share on other sites

Don't think that will work. Pretty sure you need a GuiGetMsg() in the nested while loop.

Fixed thanks, man i hate the non auto-indent in the reply box messes me up

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks for the responses, thats exactly what I needed! :P

Edit: I'm assuming that the nested GuiGetMsg would have to wait for each iteration of the while loop before exiting the loop right? If this loop could potentially take 3-5 minutes to complete, the stop button wouldnt be very responsive I'm assuming. Is there any better way to do what I'm wanting to do? Inside the loop I'm needing to send keyboard commands, and then wait certain time periods and send new commands.

Edited by durden

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $startButton
            MsgBox(0, "GUI Event", "You pressed start button!")
            $timestamp = TimerInit()  ;start timer
            While 1
                $msg = GUIGetMsg()
                If $msg = $stopButton Then ExitLoop
                If TimerDiff($timestamp) > 10000 Then ;adjust this for your pause
                    MsgBox(0, "", "10 seconds passed")
                    ;YOUR STUFF HERE
                    $timestamp = TimerInit()  ;reset the timer
                EndIf
            WEnd
            MsgBox(0, "GUI Event", "You pressed stop button!")
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
            ExitLoop
    EndSelect
WEnd

edit - better solution

Edited by xcal

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

I had the same problem tonight.

My stopbutton didn't do a thing because a loop was very busy.

Now I've put my normal events check in a function, and call that function also in the loop, that works fine.

Example:

Dim $activeloop = 0

Func myCheckEventsFunction($msg)
  Select
    Case $msg = $GUI_EVENT_CLOSE
      Exit
    Case $msg = $myStartButton
      myStartFunction()
    Case $msg = $myStopButton
      myStopFunction()
  EndSelect
EndFunc

Func myStartFunction()
  $activeloop = 1
  While someStatement
    myCheckEventsFunction(GUIGetMsg())
    if $activeloop = 0 then return 0

    ; do your loop stuff
  Wend
EndFunc

Func myStopFunction() 
  $activeloop = 0 ; the loop will stop
EndFunc

; this is the normal event catcher
While 1
  myCheckEventsFunction(GUIGetMsg())
Wend

I'd like to hear your comments about this one.

Edited by SWINX

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  
Followers 0