Merchants Posted February 20, 2010 Posted February 20, 2010 (edited) If i run a simple GUI + 2 buttons then it works fine but if i press a button 1 then run func test1() func is slow & takes time so if i want to stop (while the func test1 is still running) it does not work.. any one got ideas? expandcollapse popup#include <GUIListBox.au3> #include <Date.au3> Global $GUI, $msg, $hListBox Opt("GUICoordMode", 1) _Main() Func _Main() Local $GUI, $msg, $hListBox $GUI = GUICreate("Test program", 300, 200) $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90) GuiCtrlCreateGroup("Logs",12,12,280,110) GuiCtrlCreateGroup("Buttons",12,130,280,60) $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26) $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26) GUISetState() While 1 $msg = GUIGetMsg($GUI) Select Case $msg = $GUI_EVENT_CLOSE GUIDelete($GUI) Exit Case $msg = $Button_1 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0) test1() Case $msg = $Button_2 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0) test2() EndSelect WEnd EndFunc Func test1() Run("Notepad.exe") WinWaitActive("[Class:Notepad]") Send("Running Notpad") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) Send("{Enter}Done{!}") Sleep(800) Send("{Enter 2}Moving Notepad") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) Send(".") Sleep(500) WinMove("[Class:Notepad]", "", 10, 10, 200, 200) Sleep(900) WinMove("[Class:Notepad]", "", 50, 50, 500, 500) Sleep(500) Send("{Enter}Done{!}") Sleep(1000) WinActivate("[Class:Notepad]") Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("button does not work{!}{Enter}Func test1 is still running") Sleep(800) Send(".") Sleep(800) Send(".") Sleep(800) Send(".") Sleep(1000) Send("{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}") Sleep(5000) WinActivate("[Class:Notepad]") Send("wel i wil exit now you can press stop and exit{!}") EndFunc Func test2() Sleep(2000) Exit EndFunc Edited February 20, 2010 by Merchants
dani Posted February 20, 2010 Posted February 20, 2010 (edited) Before I try to understand what you are doing, can you explain all the Sleep() calls in test1() ? There is no need to use Sleep, you can just use WinWaitActive or WinWait if you want to be sure the window exists / is active. Ofcourse test1() is slow if you put in so many Sleep() calls... Edited February 20, 2010 by d4ni
Moderators Melba23 Posted February 20, 2010 Moderators Posted February 20, 2010 Merchants, You cannot break in on a running function and stop it in mid-flow - even a HotKey will let the function continue after it does its own thing. You need to look for a flag of some sort - somrthing like this: expandcollapse popup#include <GUIListBox.au3> #include <Date.au3> Global $GUI, $msg, $hListBox, $Button_2 Opt("GUICoordMode", 1) _Main() Func _Main() Local $GUI, $msg, $hListBox $GUI = GUICreate("Test program", 300, 200) $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90) GuiCtrlCreateGroup("Logs",12,12,280,110) GuiCtrlCreateGroup("Buttons",12,130,280,60) $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26) $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26) GUISetState() While 1 $msg = GUIGetMsg($GUI) Select Case $msg = $GUI_EVENT_CLOSE GUIDelete($GUI) Exit Case $msg = $Button_1 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0) test1() Case $msg = $Button_2 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0) test2() EndSelect WEnd EndFunc Func test1() If Not WinExists("[Class:Notepad]") Then Run("Notepad.exe") WinActivate("[Class:Notepad]") WinWaitActive("[Class:Notepad]") Send(@CRLF & @CRLF & "Running Notpad") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send("{Enter}Done{!}") Timer(800) Send("{Enter 2}Moving Notepad") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return WinMove("[Class:Notepad]", "", 10, 10, 200, 200) Timer(900) WinMove("[Class:Notepad]", "", 50, 50, 500, 500) If Timer(500) = 1 then Return Send("{Enter}Done{!}") If Timer(1000) = 1 then Return WinActivate("[Class:Notepad]") Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("button does not work{!}{Enter}Func test1 is still running") If Timer(800) = 1 then Return Send(".") If Timer(800) = 1 then Return Send(".") If Timer(800) = 1 then Return Send(".") If Timer(1000) = 1 then Return Send("{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("wel i wil exit now you can press stop and exit{!}") EndFunc Func test2() Sleep(2000) EndFunc Func Timer($iTime) $iBegin = TimerInit() While TimerDiff($iBegin) < $iTime If GUIGetMsg() = $Button_2 Then _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0) test2() Return 1 EndIf WEnd EndFunc Of course this will only work if you have lots of idle loops in the function you want to break into. 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 Â
Merchants Posted February 20, 2010 Author Posted February 20, 2010 Merchants, You cannot break in on a running function and stop it in mid-flow - even a HotKey will let the function continue after it does its own thing. You need to look for a flag of some sort - somrthing like this: expandcollapse popup#include <GUIListBox.au3> #include <Date.au3> Global $GUI, $msg, $hListBox, $Button_2 Opt("GUICoordMode", 1) _Main() Func _Main() Local $GUI, $msg, $hListBox $GUI = GUICreate("Test program", 300, 200) $hListBox = _GUICtrlListBox_Create($GUI, "", 25, 30, 255, 90) GuiCtrlCreateGroup("Logs",12,12,280,110) GuiCtrlCreateGroup("Buttons",12,130,280,60) $Button_1 = GuiCtrlCreateButton("Run", 30, 150, 100, 26) $Button_2 = GuiCtrlCreateButton("Stop and Exit", 170, 150, 100, 26) GUISetState() While 1 $msg = GUIGetMsg($GUI) Select Case $msg = $GUI_EVENT_CLOSE GUIDelete($GUI) Exit Case $msg = $Button_1 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Running: Func test1()", 0) test1() Case $msg = $Button_2 _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0) test2() EndSelect WEnd EndFunc Func test1() If Not WinExists("[Class:Notepad]") Then Run("Notepad.exe") WinActivate("[Class:Notepad]") WinWaitActive("[Class:Notepad]") Send(@CRLF & @CRLF & "Running Notpad") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send("{Enter}Done{!}") Timer(800) Send("{Enter 2}Moving Notepad") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return Send(".") If Timer(500) = 1 then Return WinMove("[Class:Notepad]", "", 10, 10, 200, 200) Timer(900) WinMove("[Class:Notepad]", "", 50, 50, 500, 500) If Timer(500) = 1 then Return Send("{Enter}Done{!}") If Timer(1000) = 1 then Return WinActivate("[Class:Notepad]") Send("{Enter 2}try to press stop and exit{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("does not work :P{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("button does not work{!}{Enter}Func test1 is still running") If Timer(800) = 1 then Return Send(".") If Timer(800) = 1 then Return Send(".") If Timer(800) = 1 then Return Send(".") If Timer(1000) = 1 then Return Send("{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("Fix it{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("still does not work{!}{Enter}zZzZ... 5 sec{Enter 2}") If Timer(5000) = 1 then Return WinActivate("[Class:Notepad]") Send("wel i wil exit now you can press stop and exit{!}") EndFunc Func test2() Sleep(2000) EndFunc Func Timer($iTime) $iBegin = TimerInit() While TimerDiff($iBegin) < $iTime If GUIGetMsg() = $Button_2 Then _GUICtrlListBox_InsertString($hListBox, "[" & _NowTime() & " ] Stop & Exit: Func test2()", 0) test2() Return 1 EndIf WEnd EndFunc Of course this will only work if you have lots of idle loops in the function you want to break into. M23 This work good thank you
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