Jump to content
Sign in to follow this  
acers

start/stop button and loop

Recommended Posts

acers

hey i've been trying to figure out how to make the loop() check if a button was pressed but with no luck. can someone shed some light on this subject? i want to exit loop() if a stop button is pressed.

$count = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Button1
            If $count = 1 Then
                GUICtrlSetData($Button1, "Start")
                $count = 0
                
            ElseIf $count = 0 Then
                GUICtrlSetData($Button1, "Stop")
                $count = 1
                Loop()

            EndIf
            
        Case $Button2
            GUISwitch($Form1_1)
            GUIDelete()
            Exit
            
    EndSwitch
WEnd

Func Loop()
EndFunc

Share this post


Link to post
Share on other sites
Zedna

Maybe something like this:

$count = 0
$stop = False

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Button1
            If $count = 1 Then
                GUICtrlSetData($Button1, "Start")
                $count = 0
                
            ElseIf $count = 0 Then
                GUICtrlSetData($Button1, "Stop")
                $count = 1
                Loop()
                If $stop Then ...

            EndIf
            
        Case $Button2
            GUISwitch($Form1_1)
            GUIDelete()
            Exit
            
    EndSwitch
WEnd

Func Loop()
    $stop = False
    While ...
        $nMsg = GUIGetMsg()
        If $nMsg = $Button1 Then
            $stop = True
            Return
        EndIf
        ...
    WEnd
EndFunc

Share this post


Link to post
Share on other sites
acers

it's not working. is it because i'm using the same button for start and stop?

edit: here is the full code. if anyone can get it to work i would be really grateful.

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Form1_1 = GUICreate("Form1", 539, 466, 192, 124)
$Button1 = GUICtrlCreateButton("Start", 88, 24, 97, 41)
$Button2 = GUICtrlCreateButton("Exit", 192, 24, 97, 41)
GUISetState(@SW_SHOW)

$count = 0

While 1
    
    $nMsg = GUIGetMsg()
    
    Switch $nMsg
        
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Button1
            If $count = 1 Then
                GUICtrlSetData($Button1, "Start")
                $count = 0
                
            ElseIf $count = 0 Then
                GUICtrlSetData($Button1, "Stop")
                $count = 1
                Loop()
                
            EndIf
            
        Case $Button2
            GUISwitch($Form1_1)
            GUIDelete()
            Exit
            
    EndSwitch
WEnd

Func Loop()
EndFunc
Edited by acers

Share this post


Link to post
Share on other sites
PsaltyDS

Once you go into Loop(), the GUI messages never get checked again. Another possibility:

#include <GUIConstantsEx.au3>

Global $Form1_1, $Button1, $Button2, $f_Run = False
$Form1_1 = GUICreate("Form1", 539, 466, 192, 124)
$Button1 = GUICtrlCreateButton("Start", 88, 24, 97, 41)
$Button2 = GUICtrlCreateButton("Exit", 192, 24, 97, 41)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            $f_Run = Not $f_Run
            
        Case $Button2
            Exit
    EndSwitch
    
    If $f_Run Then
        ; Do one cycle of whatever... 
        ; Should be something that doesn't take long or GUI will be unresponsive
    EndIf
WEnd

The stuff you do inside the "If $f_Run Then" condition can still make the GUI clunky if it takes long. In that case you want to use events instead. The easiest event is just a HotKeySet(), but if you want to use the GUI put it in GuiOnEventMode:

#include <GUIConstantsEx.au3>

Opt("GuiOnEventMode", 1)
Global $Label1, $f_Run = False

GUICreate("Form1", 539, 466, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
GUICtrlCreateButton("Start/Stop", 88, 24, 97, 41)
GUICtrlSetOnEvent(-1, "_ButtonHit")
GUICtrlCreateButton("Exit", 192, 24, 97, 41)
GUICtrlSetOnEvent(-1, "_ButtonHit")
$Label1 = GUICtrlCreateLabel("Time = 00:00:00", 88, 150, 200, 20)
GUISetState(@SW_SHOW)

While 1
    If $f_Run Then
        $sTime = "Time = " & @HOUR & ":" & @MIN & ":" & @SEC
        If GUICtrlRead($Label1) <> $sTime Then GUICtrlSetData($Label1, $sTime)
    EndIf
    Sleep(20)
WEnd

Func _quit()
    Exit
EndFunc   ;==>_Quit

Func _ButtonHit()
    Switch ControlGetText(@GUI_WinHandle, "", @GUI_CtrlHandle)
        Case "Start/Stop"
            $f_Run = Not $f_Run
        Case "Exit"
            Exit
    EndSwitch
EndFunc   ;==>_ButtonHit

Go ahead, use event mode. You know you want to!

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Gui

Try this:

#include <INet.au3>

#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#Include <GuiEdit.au3>

#include <Constants.au3>
#include <GUImenu.au3>
#include <File.au3>
#include <Array.au3>

Global $running = 0

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Toothless", 317, 140, 192, 124)
$Start = GUICtrlCreateButton("Start", ........)
$Stop = GUICtrlCreateButton("Stop", ..........)
GUIRegisterMsg($WM_COMMAND,'command')
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Func command($hwnd,$msg,$wparam,$lparam)
    $nNotifyCode = BitShift($wparam, 16)
    $nID = BitAND($wparam, 0x0000FFFF)
    If $nID = $Start Then
        $running = 1
    EndIf

    If $nID = $Stop Then
        $running = 0
    EndIF
EndFunc

While 1
    
    $nMsg = GUIGetMsg ()
    
    Switch $nMsg
            
      Case $GUI_EVENT_CLOSE
          $running = 0 
      Case $Start
           _Start()
           $running = 1
    EndSwitch
WEnd

Func _Start()
      While 1
            If $running = 0 Then Return
      Wend
EndFunc
Edited by Gui

Share this post


Link to post
Share on other sites
PsaltyDS

@Gui: Why would you use so much code to re-invent much simpler native AutoIt functions? Of course you could register and handle every message type yourself, but why would you if you don't have to?

:mellow:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Gui

Why not? Ha in my case, I do it b/c it's useful in complected loops. You could also do:

While $running = 1
;;;;
Wend

etc. This method also works with GUIGetMsg, instead of OnEvent functions, which I hate. I mean it's up to the coder lol. Just putting up my suggestion.

Also, I don't think it's simple at all. There's maybe HotKey, but other than that, completely stopping loops in AutoIt isn't that simple.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.