orbs Posted July 2, 2013 Share Posted July 2, 2013 hello world, i'm looking for some method like ExitLoop level, but for Return from functions (not confusing with function return value). i.e. ExitLoop can get you out of a loop1, and out of loop2 that loop1 is in, if you specify level 2. i want to call func1, and func1 needs to call func2, and i want func2 to return to func1 and make func1 also return to the calling script. currently i'm doing it with a condition check, but i'm looking for something more elegant, and with less code lines. Func func1() If True Then ; the condition that makes func1 call func2 func2() Return EndIf EndFunc Func func2() Return ; how to make return level 2 that will also make func1 return? EndFunc i want func1 to look like this: Func func1() If True Then func2() ; func1 will return upon func2 return, so the next lines in func1 are not executed ; do something EndFunc ok, so it does not make a lot of sense in this short example, but over a longer project, the coding difference is apparent. any ideas are welcome, thanks all. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
guinness Posted July 2, 2013 Share Posted July 2, 2013 You will have to design your own. Why not use SetError instead and using Return 2 won't return from the 2nd function. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
orbs Posted July 2, 2013 Author Share Posted July 2, 2013 i figured this much, but that will still impose a Return statement in func1 right after the call to func2, and this is what i want to avoid. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Solution trancexx Posted July 2, 2013 Solution Share Posted July 2, 2013 If True Then Return func2() ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
guinness Posted July 2, 2013 Share Posted July 2, 2013 Sorry, trancexx is on the mark. It's early, that's my excuse. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
zkenjiz Posted February 11, 2021 Share Posted February 11, 2021 After 8 years, there are no answer for this, I need it too, sad. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 11, 2021 Moderators Share Posted February 11, 2021 zkenjiz, Welcome to the AutoIt forums. The answer from trancexx a couple of posts above is the answer. 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...
Developers Jos Posted February 11, 2021 Developers Share Posted February 11, 2021 4 hours ago, zkenjiz said: After 8 years, there are no answer for this, I need it too, sad. First post and unable to read... sad. Exit 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
zkenjiz Posted February 11, 2021 Share Posted February 11, 2021 6 hours ago, Melba23 said: zkenjiz, Chào mừng đến với diễn đàn AutoIt. Câu trả lời từ trancexx một vài bài viết trên là câu trả lời. M23 Thanks, but that answer not match the question. As above question, I don't think there a many many line with "If Not _Func() Then Return" to come back main loop at anytime when event catched is a good choose. Link to comment Share on other sites More sharing options...
Developers Jos Posted February 11, 2021 Developers Share Posted February 11, 2021 Post what you have that isn't working and explain the issue. Also please stop adding Translated Quoted text in your replies! Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
zkenjiz Posted February 11, 2021 Share Posted February 11, 2021 Sorry for the translated quoted. This is an example code I using a custom sleep function to Sleep() and check event all the time. The problem I find for this situation: Return Multilevel like ExitLoop(2). If I need Exit when $event=true (anytime when Func _Sub is on running) It's simple by "If $event Then Exit", but if I need return to mainloop by "If Not _iSleep() Then Return" code is terrible , is there any better way? expandcollapse popup$callFunc = True $event = False _Main() Func _Main() While 1 If $callFunc Then _Sub() WEnd EndFunc ;==>_Main Func _Sub() ConsoleWrite('do work 1' & @CRLF) If Not _iSleep(1000) Then Return ConsoleWrite('do work 2' & @CRLF) If Not _iSleep(2000) Then Return ConsoleWrite('do work 6' & @CRLF) If Not _iSleep(500) Then Return ConsoleWrite('do work 4' & @CRLF) If Not _iSleep(3000) Then Return ConsoleWrite('do work 2' & @CRLF) If Not _iSleep(5000) Then Return ConsoleWrite('do work 1' & @CRLF) If Not _iSleep(1000) Then Return ConsoleWrite('do work 7' & @CRLF) If Not _iSleep(500) Then Return ConsoleWrite('do work a' & @CRLF) If Not _iSleep(10000) Then Return ConsoleWrite('do work c' & @CRLF) If Not _iSleep(4000) Then Return ConsoleWrite('do work g' & @CRLF) If Not _iSleep(1000) Then Return ;............ EndFunc ;==>_Sub Func _iSleep($sleepTime) Local $timer = TimerInit() While TimerDiff($timer) < $sleepTime Sleep(10) If $event Then Return False WEnd Return True EndFunc ;==>_iSleep Link to comment Share on other sites More sharing options...
Dan_555 Posted February 11, 2021 Share Posted February 11, 2021 (edited) Hi, You can use something like this: expandcollapse popup#include <Misc.au3> $callFunc = True $event = False Global $statemachine ,$quit _Main() Func _Main() While 1 If $callFunc Then _Sub() ConsoleWrite ("out" & @crlf) EndIf If $quit=1 then Return WEnd EndFunc ;==>_Main Func _Sub() $quit = 0 $statemachine=0 While $quit = 0 Switch $statemachine Case 0 ConsoleWrite('do work 1' & @CRLF) _iSleep(1000) Case 1 ConsoleWrite('do work 2' & @CRLF) _iSleep(2000) Case 2 ConsoleWrite('do work 6' & @CRLF) _iSleep(500) Case 3 ConsoleWrite('do work 4' & @CRLF) _iSleep(3000) Case 4 ConsoleWrite('do work 2a' & @CRLF) _iSleep(5000) Case 5 ConsoleWrite('do work 1a' & @CRLF) _iSleep(1000) Case 6 ConsoleWrite('do work 7' & @CRLF) _iSleep(500) Case 7 ConsoleWrite('do work a' & @CRLF) _iSleep(10000) Case 8 ConsoleWrite('do work c' & @CRLF) _iSleep(4000) Case 9 ConsoleWrite('do work g' & @CRLF) _iSleep(1000) Case 10 $statemachine=0 ;............ EndSwitch WEnd $callFunc=0 EndFunc ;==>_Sub Func _iSleep($sleepTime) Local $timer = TimerInit() While TimerDiff($timer) < $sleepTime Sleep(10) If _isPressed("1b") Then $quit = 1 $statemachine = 1000 Return False EndIf WEnd $statemachine=$statemachine+1 Return True EndFunc ;==>_iSleep Of course you can skip the Switch/Endswitch and use If Not _iSleep(1000) Then Return as well. Edited February 11, 2021 by Dan_555 Some of my script sourcecode Link to comment Share on other sites More sharing options...
Nine Posted February 11, 2021 Share Posted February 11, 2021 Another way : expandcollapse popup$callFunc = True $event = False _Main() Func _Main() While $callFunc _Sub() WEnd EndFunc ;==>_Main Func _Sub() Local $aWork = [[_Work1,1000],[_Work2,2000],[_Work6,500]] ;..... For $i = 0 to UBound($aWork) - 1 $aWork[$i][0]() If Not _iSleep($aWork[$i][1]) Then Return Next EndFunc ;==>_Sub Func _iSleep($sleepTime) Local $timer = TimerInit() While TimerDiff($timer) < $sleepTime Sleep(10) If $event Then Return False WEnd Return True EndFunc ;==>_iSleep Func _Work1() ConsoleWrite ("job1" & @CRLF) Return EndFunc Func _Work2() ConsoleWrite ("job2" & @CRLF) Return EndFunc Func _Work6() ConsoleWrite ("job6" & @CRLF) Return EndFunc “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
zkenjiz Posted February 11, 2021 Share Posted February 11, 2021 @Dan_555 My _Sub func script is a continuous command in long time, not a switch or short loop did you know, that's problem. If it's a shot loop then I don't care about multi level return because only need few or maybe onetime check return bool value from _iSleep each loop. Link to comment Share on other sites More sharing options...
junkew Posted February 11, 2021 Share Posted February 11, 2021 1. Can you give an example on what you need from any other programming language? 2. I think what @Nine has given is an elegant programming style to make for each job a worker function which is dispatched with assigning a function to a variable Personally I would feel returning multiple nested levels back will make your code hard to maintain and understand. Even exitloop nn should be used with care FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
Dan_555 Posted February 11, 2021 Share Posted February 11, 2021 (edited) expandcollapse popup#include <Misc.au3> $callFunc = True $event = False Global $quit _Main() Func _Main() While 1 If $callFunc Then _Sub() ConsoleWrite ("out" & @crlf) EndIf If $quit=1 then Return WEnd EndFunc ;==>_Main Func _Sub() $quit = 0 While $quit = 0 ConsoleWrite('do work 1' & @CRLF) If Not _iSleep(1000) Then Return ConsoleWrite('do work 2' & @CRLF) If Not _iSleep(2000) Then Return ConsoleWrite('do work 6' & @CRLF) If Not _iSleep(500) Then Return ConsoleWrite('do work 4' & @CRLF) If Not _iSleep(3000) Then Return ConsoleWrite('do work 2' & @CRLF) If Not _iSleep(5000) Then Return ConsoleWrite('do work 1' & @CRLF) If Not _iSleep(1000) Then Return ConsoleWrite('do work 7' & @CRLF) If Not _iSleep(500) Then Return ConsoleWrite('do work a' & @CRLF) If Not _iSleep(10000) Then Return ConsoleWrite('do work c' & @CRLF) If Not _iSleep(4000) Then Return ConsoleWrite('do work g' & @CRLF) If Not _iSleep(1000) Then Return WEnd $callFunc=0 EndFunc ;==>_Sub Func _iSleep($sleepTime) Local $timer = TimerInit() While TimerDiff($timer) < $sleepTime Sleep(10) If _isPressed("1b") Then ;Check for the escape key. $quit = 1 Return False EndIf WEnd Return True EndFunc ;==>_iSleep As said previously, it works even with your sample code. Start it, then press esc, and it jumps out of the two sub's. Edited February 11, 2021 by Dan_555 removed the $statemachine refference Some of my script sourcecode Link to comment Share on other sites More sharing options...
zkenjiz Posted February 12, 2021 Share Posted February 12, 2021 (edited) 8 hours ago, Dan_555 said: As said previously, it works even with your sample code. Start it, then press esc, and it jumps out of the two sub's. Right, that code work but in fact the code is not so few lines, anytime I call _iSleep() I must type "If Not _iSleep() Then Return" - every where every time, so it not ok because _iSleep() using like Sleep and I call it many times in script, if somethings exitst like Return(2) from inside _iSleep(), I can only type _iSleep(). Of course, I think this need comes from the lack of multiprocess, so its not need in other language like C# example. Edited February 12, 2021 by zkenjiz Link to comment Share on other sites More sharing options...
junkew Posted February 12, 2021 Share Posted February 12, 2021 Basic rule of programming: If you feel you are writing the same to frequently probably time to refactor your coding style No multiprocess is not an excuse as you can see in @nine 's answer there is an elegant way to have it handled in a maintainable and understandable way FAQ 31 How to click some elements, FAQ 40 Test automation with AutoIt, Multithreading CLR .NET Powershell CMDLets Link to comment Share on other sites More sharing options...
zkenjiz Posted February 12, 2021 Share Posted February 12, 2021 (edited) 20 minutes ago, junkew said: Basic rule of programming: If you feel you are writing the same to frequently probably time to refactor your coding style No multiprocess is not an excuse as you can see in @nine 's answer there is an elegant way to have it handled in a maintainable and understandable way I working with AutoIt over 10 years, similar with C# or Java, so I don't thing my coding style is problem here, not all situation can change to using switch or loop sequence, especially in script programming. It's difficult to understand the need here if you are not really in that situation. Ok, so I thing there's no right answer for finding multi level return like exitloop. Thanks all! Edited February 12, 2021 by zkenjiz 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