Sign in to follow this  
Followers 0
Markus21

Interrupt a loop by clicking button

4 posts in this topic

Hello,

follow situation:

I have got two buttons "start" and "stop" and one input field "delay". When start is clicked, a function is called, where it should be looped until the stop button has been clicked. The Problem is, that the GUI doenst react anymore, when Im in that loop.

The program should send clicks every "delay" seconds until "stop" has been clicked.

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $running;
$running = False;

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("DF Loop", 150, 80)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")

GUICtrlCreateLabel("Delay:",20,20)
GUICtrlCreateLabel("ms",110,20)
$delay = GUICtrlCreateInput("",55,17,50)
$start = GUICtrlCreateButton("Start",20,44,60)
$stop = GUICtrlCreateButton("Stop",80,44,60)

GUICtrlSetState($stop,$GUI_DISABLE)

GUICtrlSetOnEvent($start,"start")
GUICtrlSetOnEvent($stop,"stop")


GUISetState(@SW_SHOW)

While (1)
    sleep(1000)
WEnd

Func close()
    Exit
EndFunc

Func start()
    $running = True
    $ms = GUICtrlRead($delay)
    GUICtrlSetState($start,$GUI_DISABLE)
    GUICtrlSetState($stop,$GUI_ENABLE)
    
    while($running)
        If (WinActive("bla")) Then
            MouseClick("left")
        EndIf
        sleep($ms)
    WEnd
    GUICtrlSetState($start,$GUI_ENABLE)
EndFunc

Func stop()
    GUICtrlSetState($stop,$GUI_DISABLE)
    $running = False
EndFunc

I need some kind of multi-threaded solution for this or a workaroud. I could skip the sleep by working with a timer. but then I have got a While(1) loop- which has the same effect on the GUI.

Thank you for help!

Markus

Share this post


Link to post
Share on other sites



Hi,

this make the problem:

while($running)
        If (WinActive("bla")) Then
            MouseClick("left")
        EndIf
        sleep($ms)
    WEnd

Better way: use AdlibEnable with $ms as time for repeat.


Best Regards BugFix  

Share this post


Link to post
Share on other sites

Markus21,

The problem is that you cannot call a function until the current fuction has finished - so when you press the "Stop" button, it is waiting for the "Start" function to end - which is itself waiting for the "Stop" function to end it!

2 possibilites:

1. Use Adlib to call your function every $ms ms. "Start"/"Stop" would just enable/disable Adlib.

2. Add a GUIGetMsg loop inside your function. You can mix the GUI modes as long as you remember to reset the mode as required. This code shows what I mean - I have added a few lines so you can see the loop working and then ending:

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $running;
$running = False;

Opt("GUIOnEventMode", 1)

$GUI = GUICreate("DF Loop", 150, 80)
GUISetOnEvent($GUI_EVENT_CLOSE, "close")

GUICtrlCreateLabel("Delay:",20,20)
GUICtrlCreateLabel("ms",110,20)
$delay = GUICtrlCreateInput("",55,17,50)
$start = GUICtrlCreateButton("Start",20,44,60)
$stop = GUICtrlCreateButton("Stop",80,44,60)

GUICtrlSetState($stop,$GUI_DISABLE)

GUICtrlSetOnEvent($start,"start")
;GUICtrlSetOnEvent($stop,"stop")

GUISetState(@SW_SHOW)

While (1)
    sleep(1000)
WEnd

Func close()
    Exit
EndFunc

Func start()
    $running = True
    $ms = GUICtrlRead($delay)
    GUICtrlSetState($start,$GUI_DISABLE)
    GUICtrlSetState($stop,$GUI_ENABLE)
    
    Opt("GUIOnEventMode", 0)
    
    $begin = TimerInit()
    $count = 1
    
    while($running)
        If (WinActive("bla")) Then
            MouseClick("left")
        EndIf
        
        ConsoleWrite("Running " & $count & @CRLF)
        
        While TimerDiff($begin) < $ms
        
            If GUIGetMsg() = $stop Then
                stop()
                Opt("GUIOnEventMode", 1)
                GUICtrlSetState($start,$GUI_ENABLE)
                ConsoleWrite("Stopping" & @CRLF)
                ExitLoop 2
            EndIf
        
        WEnd
        
        $begin = TimerInit()
        $count += 1
        
    WEnd
    Opt("GUIOnEventMode", 1)
    
    GUICtrlSetState($start,$GUI_ENABLE)
EndFunc

Func stop()
    GUICtrlSetState($stop,$GUI_DISABLE)
    $running = False
EndFunc

I hope this is helpful.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Thanks Melba and BugFix, I tried the same, but I forgot to Opt("GUIOnEventMode", 0).

By the way, is there also a function, which sends a certain key to a window? (not to a specific control element)

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