Markus21 Posted March 8, 2009 Posted March 8, 2009 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. expandcollapse popup#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
BugFix Posted March 8, 2009 Posted March 8, 2009 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
Moderators Melba23 Posted March 8, 2009 Moderators Posted March 8, 2009 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:expandcollapse popup#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 EndFuncI 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Markus21 Posted March 8, 2009 Author Posted March 8, 2009 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now