Jump to content

Loop problem


sanhen
 Share

Recommended Posts

Once i press button start function...

Press the button again to stop function ...

I would like to ask how to achieve?

#include <GUIConstants.au3>
#include <ButtonConstants.au3>
Opt("GUIOnEventMode", 1)

$Stop = 0
    
$hGUI=GUICreate ("test", 300,120,-1,-1)
GUISetOnEvent($GUI_EVENT_CLOSE, "GuiMsg")

$button1 = GUICtrlCreateCheckbox("test", 100, 20, 80, 25,$BS_PUSHLIKE)
GUICtrlSetOnEvent($button1, "ControlMsg")

GUISetState()

While 1
    
Sleep(1000)

Wend



Func GuiMsg()
    Switch @GUI_CTRLID 
        Case $GUI_EVENT_CLOSE
            Exit                    
        Case $GUI_EVENT_MINIMIZE
            guisetstate(@SW_MINIMIZE)
    EndSwitch
EndFunc


Func ControlMsg()
    
    If BitAND(GuiCtrlRead(@GUI_CtrlId), $GUI_CHECKED) Then
        GUICtrlSetData(@GUI_CtrlId,"11111")
        $Stop = 0
        _test()
    Else
        GUICtrlSetData(@GUI_CtrlId,"22222")
        $Stop = 1
    EndIf
    
EndFunc



Func _test()
    
    $i =0
        Do
            $i+=1
            GUICtrlSetData($button1,$i)
            Sleep(500)
        Until $Stop = 1 or $i=1000000
        
EndFunc
Link to comment
Share on other sites

  • Moderators

@sanhen,

I believe that you cannot recall a function if it is already running. So if you call your _test() function from within the ControlMsg() function, you cannot "re"call the ControlMsg() function until it has executed. Therefore you need to place the call to _test() outside ControlMsg() to enable it to return. The following shows what I mean:

#include <GUIConstants.au3>
#include <ButtonConstants.au3>
Opt("GUIOnEventMode", 1)

$Stop = 0
    
$hGUI=GUICreate ("test", 300,120,-1,-1)
GUISetOnEvent($GUI_EVENT_CLOSE, "GuiMsg")

$button1 = GUICtrlCreateCheckbox("test", 100, 20, 80, 25,$BS_PUSHLIKE)
GUICtrlSetOnEvent($button1, "ControlMsg")

$label1 = GUICtrlCreateLabel("", 100, 70, 80, 25)

GUISetState()

While 1
    
    Sleep(100)

    If $Stop = 0 Then _test()

Wend



Func GuiMsg()
    Switch @GUI_CTRLID 
        Case $GUI_EVENT_CLOSE
            Exit                
        Case $GUI_EVENT_MINIMIZE
            guisetstate(@SW_MINIMIZE)
    EndSwitch
EndFunc


Func ControlMsg()
   
    If BitAND(GuiCtrlRead($button1), $GUI_CHECKED) = $GUI_CHECKED Then
        $Stop = 0
    Else
        $Stop = 1
    EndIf
    
EndFunc



Func _test()
    
    $i =0
        Do
            $i+=1
            GUICtrlSetData($button1, $i)
            Sleep(500)
        Until $Stop = 1 or $i=1000000
        
EndFunc

Hope this helps,

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@sanhen

This discussed many times:

Easy method (not reliable, need a thick loop)

#include <GUIConstants.au3>
#include <ButtonConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("test", 300, 120, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "GuiMsg")

$button1 = GUICtrlCreateCheckbox("test", 100, 20, 80, 25, $BS_PUSHLIKE)
GUICtrlSetOnEvent($button1, "ControlMsg")

GUISetState()

While 1
    Sleep(1000)
WEnd

Func GuiMsg()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_MINIMIZE
            GUISetState(@SW_MINIMIZE)
    EndSwitch
EndFunc   ;==>GuiMsg

Func ControlMsg()
    If BitAND(GUICtrlRead(@GUI_CtrlId), $GUI_CHECKED) Then
        GUICtrlSetData(@GUI_CtrlId, "11111")
        _test()
    Else
        GUICtrlSetData(@GUI_CtrlId, "22222")
    EndIf
EndFunc   ;==>ControlMsg

Func _test()
    Local $iOld_EventMode = Opt("GUIOnEventMode", 0)
    Local $i = 0
    
    While 1
        If (GUIGetMsg() = $button1) Or ($i = 1000000) Then ExitLoop
        $i += 1
        GUICtrlSetData($button1, $i)
        Sleep(300)
    WEnd
    
    GUICtrlSetData($button1, "22222")
    Opt("GUIOnEventMode", $iOld_EventMode)
EndFunc   ;==>_testoÝ÷ Øojwvg­kzXnWºÚ"µÍÚ[ÛYH ÑÕRPÛÛÝ[Ë]LÉÝÂÚ[ÛYH  Ð]ÛÛÛÝ[Ë]LÉÝÂÚ[ÛYH   ÕÚ[ÝÜÐÛÛÝ[Ë]LÉÝÂÜ
    ][ÝÑÕRSÛ][[ÙI][ÝËJBÛØ[ ÌÍÔÝÜÌÍÚÕRHHÕRPÜX]][ÝÝÝ    ][ÝËÌLLKLJBÕRTÙ]Û][
    ÌÍÑÕRWÑUSÐÓÔÑK ][ÝÑÝZSÙÉ][ÝÊBÌÍØ]ÛHHÕRPÝÜX]PÚXÚØÞ
    ][ÝÝÝ    ][ÝËLK    ÌÍÐ×ÔTÒRÑJBÕRPÝÙ]Û][
    ÌÍØ]ÛK  ][ÝÐÛÛÛÙÉ][ÝÊBÕRTYÚÝÙÊ    ÌÍÕÓWÐÓÓSPS  ][Ý×ÕÓWÐÓÓSPS    ][ÝÊBÕRTÙ]Ý]J
BÚ[HBTÛY
L
BÑ[[ÈÕÓWÐÓÓSPS
    ÌÍÚÛ    ÌÍÓÙË  ÌÍÝÔ[K  ÌÍÛ[JBSØØ[ ÌÍÛÛÛÛH]S
    ÌÍÝÔ[KBSØØ[   ÌÍÛÝYHH]ÚY
    ÌÍÝÔ[KMBBTÝÚ]Ú   ÌÍÛÛÛÛBPØÙH ÌÍØ]ÛBBBTÝÚ]Ú    ÌÍÛÝYBBBBPØÙH ÌÍÐÐÓPÒÑQBBBBRY]S
ÕRPÝXY
    ÌÍÛÛÛÛ
K   ÌÍÑÕRWÐÒPÒÑQ
H[BBBBBIÌÍÔÝÜHBBBBQ[ÙBBBBBBIÌÍÔÝÜHBBBBBQ[YBBQ[ÝÚ]ÚQ[ÝÚ]ÚBT]    ÌÍÑÕRWÔSQTÑÂ[[ÈÏOIÝ×ÕÓWÐÓÓSPS[ÈÝZSÙÊ
BTÝÚ]ÚÕRWÐÝYBPØÙH   ÌÍÑÕRWÑUSÐÓÔÑBBBQ^]BPØÙH ÌÍÑÕRWÑUSÓRSSRVBBBQÕRTÙ]Ý]JÕ×ÓRSSRVJBQ[ÝÚ]Ú[[ÈÏOIÝÑÝZSÙÂ[ÈÛÛÛÙÊ
BRY]S
ÕRPÝXY
ÕRWÐÝY
K   ÌÍÑÕRWÐÒPÒÑQ
H[BQÕRPÝÙ]]JÕRWÐÝY    ][ÝÌLLLLI][ÝÊBBWÝÝ

BQ[ÙBBQÕRPÝÙ]]JÕRWÐÝY    ][ÝÌ][ÝÊBQ[Y[[ÈÏOIÝÐÛÛÛÙÂ[ÈÝÝ

BSØØ[ ÌÍÚHHBUÚ[HBBRY
    ÌÍÔÝÜHJHÜ
    ÌÍÚHHL
H[^]ÛÜBIÌÍÚH
ÏHBBQÕRPÝÙ]]J   ÌÍØ]ÛK  ÌÍÚJBBTÛY
Ì
BUÑ[BNÉÌÍÔÝÜH[[ÈÏOIÝ×ÝÝ

:)

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