simbo Posted November 28, 2008 Share Posted November 28, 2008 Hello All. I'd like my users to be able to stop one of my functions part-way through executing if they get fed-up waiting. I've currently got something like this:- guicreate("My GUI,",200,200) guisetstate(@SW_SHOW) $button=guictrlcreatebutton("Click here to process data",-1,-1) do $msg=guigetmsg() select case $msg=$GUI_EVENT_CLOSE exit case $msg=$button call ("process_data") endselect until 1=0 func process_data() msgbox(0,"Processing","This may take a while, press ESCAPE if you can't wait") for $i=1 to 100 ;perform long, tedious calculations next endfunc How can I acheive this? I use hotkeys a lot, but AFAIK, I can only use them to call a function, whereas what I want is to return from my function to my main loop. Sorry if this is a really amateur question! Thanks in advance Matt Link to comment Share on other sites More sharing options...
goldenix Posted November 28, 2008 Share Posted November 28, 2008 (edited) as far as I know there is no universal way doing it, so Cheat.make a global variable & put switches inside your function. so if user presses a hotkey you change the global variable value to true or false.EDITED!Something like thatGlobal $Exitfunc HotKeySet("{space}", "Toggle") While 1 _main() WEnd Func _main() $Exitfunc = False For $a= 1 to 100 Sleep(500) If $Exitfunc = True then Return ToolTip($a) Next EndFunc Func Toggle() $Exitfunc = True ToolTip('Abort') EndFunc Edited November 28, 2008 by goldenix My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list] Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 28, 2008 Share Posted November 28, 2008 Hello All. I'd like my users to be able to stop one of my functions part-way through executing if they get fed-up waiting. I've currently got something like this:- guicreate("My GUI,",200,200) guisetstate(@SW_SHOW) $button=guictrlcreatebutton("Click here to process data",-1,-1) do $msg=guigetmsg() select case $msg=$GUI_EVENT_CLOSE exit case $msg=$button call ("process_data") endselect until 1=0 func process_data() msgbox(0,"Processing","This may take a while, press ESCAPE if you can't wait") for $i=1 to 100 ;perform long, tedious calculations next endfunc How can I acheive this? I use hotkeys a lot, but AFAIK, I can only use them to call a function, whereas what I want is to return from my function to my main loop. Sorry if this is a really amateur question! Thanks in advance Matt You can interrupt a function, but the script will return to it eventually. So while it is interrupted, some kind of status or flag needs to change that will tell the function to return (exit the function). In this version, I use the $fEscaped flag global variable. The loop that simulates your long function must periodically test the condition of $fEscaped to see if it should return early. The HotKeySet() is only enabled while the function is running. expandcollapse popup#include <GuiConstantsEx.au3> Global $fEscaped = False, $msg Global $hGUI = GUICreate("My GUI,", 300, 200) Global $label = GUICtrlCreateLabel("Click START to begin...", 10, 10, 280, 20) Global $button = GUICtrlCreateButton("START", 100, 150, 100, 30) GUISetState(@SW_SHOW) Do $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $msg = $button process_data() EndSelect Until 0 Func process_data() Local $iTimer MsgBox(0, "Processing", "This may take a while, press ESCAPE if you can't wait") HotKeySet("{ESC}", "_Escaped") For $i = 100 To 1 Step -1 GUICtrlSetData($label, "Count down = " & $i) $iTimer = TimerInit() Do Sleep(20) If $fEscaped Then GUICtrlSetData($label, "Escaped at count down = " & $i) $fEscaped = False Return EndIf Until TimerDiff($iTimer) >= 1000 Next HotKeySet("{ESC}") GUICtrlSetData($label, "Click START to begin...") EndFunc ;==>process_data Func _Escaped() $fEscaped = True HotKeySet("{ESC}") EndFunc Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law Link to comment Share on other sites More sharing options...
simbo Posted November 28, 2008 Author Share Posted November 28, 2008 Thank you both very much. I see you're both thinking on similar lines. I can see this is going to be useful in all sorts of situations. Right, back to my late-night coding Thanks again Link to comment Share on other sites More sharing options...
Tomb Posted November 28, 2008 Share Posted November 28, 2008 if it works the same as c++, return should stop executing a function. Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 29, 2008 Share Posted November 29, 2008 if it works the same as c++, return should stop executing a function.It does, but there is no mechanism for an interrupting function (like a HotKeySet() or GUI Event) to force the return from the interrupted function. That's what makes the global flag useful. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law 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