iorih0406 Posted May 11, 2010 Share Posted May 11, 2010 (edited) Hi All, I am a beginner on the area of AutoIT. I put two buttons upon my GUI. the two buttons are "Start" and "Stop". When I press start button to run function. Meanwhile, I hope I can pause the function if I press "stop" button. But It seems not work. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstants.au3> #include <GuiEdit.au3> #include <GuiComboBox.au3> #include <buttonconstants.au3> HotKeySet("{ESC}", "alldone") $Form2 = Guicreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS)) $Edit1 = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL)) $Start = GuiCtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON) $Stop = GuiCtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON) GUICtrlSetColor(-1, 0x008080) GUISetState(@SW_SHOW) Events() GUICtrlSetState($Edit1, $GUI_FOCUS) Func Events() Opt("GUIOnEventMode", 1) GUICtrlSetOnEvent($Start, "SendEvent") GUICtrlSetOnEvent($Stop, "stopscript") EndFunc Func stopscript() while 1 $msg = GUIGetMsg() If $msg <> $Start Then sleep (30000000) Else ExitLoop EndIf WEnd EndFunc Func SendEvent() For $index = 1 To 50 Step 1 Send("123" & @CR) Sleep(3000) Send("123" & @CR) Sleep(3000) EndFunc Edited May 11, 2010 by iorih0406 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 11, 2010 Moderators Share Posted May 11, 2010 iorih0406,Welcome to the AutoIt forum. Unfortunately you cannot break into a running function as AutoIt queues the calls so that the "stopscript" will only ever be called once the "SendEvent" function has terminated.There are 2 ways around this if (like in this case) there are suitable pauses in the function:- 1. You can use MessageLoop mode and look for the Stop button while you are paused:expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstants.au3> #include <GuiEdit.au3> #include <GuiComboBox.au3> #include <buttonconstants.au3> ;HotKeySet("{ESC}", "alldone") $Form2 = GUICreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS)) $Edit1 = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL)) $Start = GUICtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON) $Stop = GUICtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON) GUICtrlSetColor(-1, 0x008080) GUISetState(@SW_SHOW) GUICtrlSetState($Edit1, $GUI_FOCUS) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Start SendEvent() EndSwitch WEnd Func SendEvent() ConsoleWrite("Starting" & @CRLF) For $index = 1 To 50 Step 1 ConsoleWrite("Send 1" & @CRLF) ;Send("123" & @CR) $iBegin = TimerInit() While TimerDiff($iBegin) < 3000 If GUIGetMsg() = $Stop Then ConsoleWrite("Stopping" & @CRLF) Return EndIf Sleep(10) WEnd ;Sleep(3000) ConsoleWrite("Send 2" & @CRLF) ;Send("123" & @CR) $iBegin = TimerInit() While TimerDiff($iBegin) < 3000 If GUIGetMsg() = $Stop Then ConsoleWrite("Stopping" & @CRLF) Return EndIf Sleep(10) WEnd ;Sleep(3000) Next EndFunc ;==>SendEvent- 2. You can keep OnEvent mode and use a HotKey to set a flag - here I have used F1. This works because HotKeys do break into running functions - but the function restarts once the HotKey function terminates :expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstants.au3> #include <GuiEdit.au3> #include <GuiComboBox.au3> #include <buttonconstants.au3> Global $fStop = False HotKeySet("{ESC}", "alldone") HotKeySet("{F1}", "_stopscript") $Form2 = GUICreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS)) GUISetOnEvent($GUI_EVENT_CLOSE, "alldone") $Edit1 = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL)) $Start = GUICtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON) ;$Stop = GUICtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON) ;GUICtrlSetColor(-1, 0x008080) GUISetState(@SW_SHOW) Events() GUICtrlSetState($Edit1, $GUI_FOCUS) While 1 Sleep(10) WEnd Func Events() Opt("GUIOnEventMode", 1) GUICtrlSetOnEvent($Start, "SendEvent") EndFunc Func SendEvent() ConsoleWrite("Starting" & @CRLF) For $index = 1 To 50 Step 1 ConsoleWrite("Send 1" & @CRLF) ;Send("123" & @CR) $iBegin = TimerInit() While TimerDiff($iBegin) < 3000 If $fStop = True Then ConsoleWrite("Stopping" & @CRLF) $fStop = False Return EndIf Sleep(10) WEnd ;Sleep(3000) ConsoleWrite("Send 2" & @CRLF) ;Send("123" & @CR) $iBegin = TimerInit() While TimerDiff($iBegin) < 3000 If $fStop = True Then ConsoleWrite("Stopping" & @CRLF) $fStop = False Return EndIf Sleep(10) WEnd ;Sleep(3000) Next EndFunc ;==>SendEvent Func _stopscript() $fStop = True EndFunc Func alldone() Exit EndFuncYou need to look at the syntax of your Send function by the way - you do not Send @CR like that! I hope that is all clear - please ask if not. 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 Link to comment Share on other sites More sharing options...
hawky358 Posted May 11, 2010 Share Posted May 11, 2010 (edited) you do not Send @CR like that! Hey M23,What's wrong with that syntax?Do you mean as opposed to @CRLF?I often use @CR when I want to send only @CR, or am I missing something?edit: Ugh brackets... Edited May 11, 2010 by hawky358 Link to comment Share on other sites More sharing options...
Kuchiha Posted May 11, 2010 Share Posted May 11, 2010 The send function is done to send keystrokes. @CR is not associated to a key, it's a macro for carriage return. Use this, it's better : Send("123{ENTER}") Sleep(3000) Link to comment Share on other sites More sharing options...
hawky358 Posted May 11, 2010 Share Posted May 11, 2010 (edited) Yea @CR = Chr(13) which is an acceptable send() In any event ok I get what you're saying, in this situation iorih probably wanted to send an ENTER. I sometimes just use @CR to signal the end of a command over TCP or doing a send to a certain window. Edited May 11, 2010 by hawky358 Link to comment Share on other sites More sharing options...
ShawnW Posted May 11, 2010 Share Posted May 11, 2010 This works because HotKeys do break into running functions - but the function restarts once the HotKey function terminates : I was curious because I didn't know this part about the function restarting. I wrote a simple test and it does not seem to restart. HotKeySet("{ESC}","Pause") Func Pause() Sleep(3000) EndFunc Func Loop() For $i = 100000 To 0 Step -1 ToolTip($i) Next EndFunc Loop() The loop runs and begins counting, Esc pauses, 3 seconds later the counting resumes right where it left off. No restarting. Link to comment Share on other sites More sharing options...
hawky358 Posted May 11, 2010 Share Posted May 11, 2010 That's what he meant, not restarting but continuing Link to comment Share on other sites More sharing options...
ShawnW Posted May 11, 2010 Share Posted May 11, 2010 Ahh okay, it was a misinterpretation. I was about to be totally mind blown if I had never realized functions restarting after a hotkey interrupt was called. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 11, 2010 Moderators Share Posted May 11, 2010 ShawnW,Sorry if I confused you. hawky358 is correct.To make it absolutely clear:A HotKey will (in nearly all cases*) interrupt a running function. Once the HotKey function ends, the original function resumes at the point it was halted.I hope that resolves the matter. M23* Read the Help file to see when it does not. 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 Link to comment Share on other sites More sharing options...
iorih0406 Posted May 12, 2010 Author Share Posted May 12, 2010 Thanks for everyone. I try to revise my code, just like solution 1. M23 posted. It's working fine. Thanks again. Link to comment Share on other sites More sharing options...
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