Sign in to follow this  
Followers 0
jacekr

Problem in exiting loop using button

2 posts in this topic

Hello

I have a simple GUI program, that reads numbers from another prorgam.

I have created two buttons, "start" and "stop".

start button starts to read the numbers from another program in the loop

and stop button is designed to end this loop and stop reading the numbers

...
GUISetState()

; Run the GUI until the dialog is closed
    
While 1
    $msg = GUIGetMsg()
    Select
      Case $msg = $start_button
        While ($stop_request==0)
          read_numbers()            
        WEnd
                
      Case $msg = $stop_button
        $stop_request=1
            clear_results()
        
    EndSelect
    
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
...

and the question is:

How can I break while loop in start_button (set $stop_request to 1) using clear_result() function (pressing stop button) ???

When the program is executing while loop after I press start button, stop_button does not work, why?

best regards

JR

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

There's multiple ways you can do what your after.

The problem your having is once you start your loop in a loop the outer loop can't register a control being clicked while the inner loop is still executing.

So you need to check for controls being clicked while your inner loop is still running.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $hGui, $Stats_display, $start_button, $stop_button, $msg

$hGui = GUICreate("", 170, 30, -1, -1)
$Stats_display = GUICtrlCreateLabel("0", 5, 5, 50, 20)
$start_button = GUICtrlCreateButton("Satrt", 60, 5, 50, 20)
$stop_button = GUICtrlCreateButton("Stop", 115, 5, 50, 20)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState(@SW_SHOW, $hGui)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start_button
            GUICtrlSetState($start_button, $GUI_DISABLE)
            GUICtrlSetState($stop_button, $GUI_ENABLE)

            ;check to see if the Stop button is pressed while the inner loop is running
            While GUIGetMsg() <> $stop_button
                read_numbers()
                Sleep(20) ;small sleep to save excess cpu usage
            WEnd

            ;If I'm here then the stop button has been clicked
            GUICtrlSetState($start_button, $GUI_ENABLE)
            GUICtrlSetState($stop_button, $GUI_DISABLE)
            clear_results()
    EndSwitch
WEnd

Func read_numbers()
    GUICtrlSetData($Stats_display, GUICtrlRead($Stats_display) + 1)
EndFunc   ;==>read_numbers

Func clear_results()
    GUICtrlSetData($Stats_display, 0)
EndFunc   ;==>clear_results

Other ways maybe you could user timer functions or adlib functions so you can leave the Main loop to catch the control clicks and the timer/adlib functions can look after constantly refreshing the data until the main loop catches a control click.

Cheers

Edited by smashly

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