Bishop12 Posted December 16, 2019 Share Posted December 16, 2019 (edited) Hi Guys can i ask im doing something on my scripts then i have Recursion i know i call function with it self without returning to first state yes i understand it while reading autoit last day now so i want to ask Recursion can solve with this kind of function Example below can i fix Recursion with that kind script? the reason is i want to run the script for long hours i hope i am right now Thankyou i nadvance NEWBIE only hehe .. i know it can be fix but im curious on Return to function im not that much familiar in auoit i dont know if Recursion can break if using RETURN + Function or Return to first function expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 Global $Returner Working1() Case $Button2 EndSwitch WEnd Func Working1() MsgBox(0,"","Step1") sleep(1000) Working2() MsgBox(0,"","STEP3") return Working1() EndFunc Func Working2() if $counter = 1 Then sleep(1000) MsgBox(0,"","STEP2") working3() Return ElseIf $counter = 0 Then $counter = $counter+1 working2() EndIf EndFunc Func working3() sleep(1000) MsgBox(0,"","Working3") Return EndFunc Edited December 16, 2019 by Bishop12 Link to comment Share on other sites More sharing options...
Nine Posted December 16, 2019 Share Posted December 16, 2019 You have to master recursion in order to use it efficiently. Your working1 function is obviously deficient, it is calling itself endlessly. If you want to benefit from recursion, the underlying algorithm must follow a precise path, which is not clear in your example. “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...
Bishop12 Posted December 16, 2019 Author Share Posted December 16, 2019 2 hours ago, Nine said: You have to master recursion in order to use it efficiently. Your working1 function is obviously deficient, it is calling itself endlessly. If you want to benefit from recursion, the underlying algorithm must follow a precise path, which is not clear in your example. @Nine yes sir i probably need to master it but can you give me example or can you correct my Example on how can i do it so i can understand it clearly on how to fix recursion simple explanation or example would be apreciated so much Link to comment Share on other sites More sharing options...
Bishop12 Posted December 16, 2019 Author Share Posted December 16, 2019 6 hours ago, Bishop12 said: Do you think making While loop in Case before Function call and return to while loop and call function again in while loop can be ? expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 while true Working1() WEnd Case $Button2 EndSwitch WEnd Func Working1() MsgBox(0,"","Step1") sleep(1000) Working2() MsgBox(0,"","STEP3") return EndFunc Func Working2() if $counter = 1 Then sleep(1000) MsgBox(0,"","STEP2") working3() Return ElseIf $counter = 0 Then $counter = $counter+1 working2() EndIf EndFunc Func working3() sleep(1000) MsgBox(0,"","Working3") Return EndFunc Link to comment Share on other sites More sharing options...
Developers Jos Posted December 16, 2019 Developers Share Posted December 16, 2019 (edited) You are still calling working2() inside of func working2() so obviously this could cause issues. What exactly are you expecting to happen there? (also try keeping you script readable by formatting it properly! Tidy can help there. ) expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 While True Working1() WEnd Case $Button2 EndSwitch WEnd Func Working1() MsgBox(0, "", "Step1") Sleep(1000) Working2() MsgBox(0, "", "STEP3") Return EndFunc ;==>Working1 Func Working2() If $counter = 1 Then Sleep(1000) MsgBox(0, "", "STEP2") working3() Return ElseIf $counter = 0 Then $counter = $counter + 1 Working2() ; <=== this is a problem EndIf EndFunc ;==>Working2 Func working3() Sleep(1000) MsgBox(0, "", "Working3") Return EndFunc ;==>working3 Jos Edited December 16, 2019 by 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...
Bishop12 Posted December 16, 2019 Author Share Posted December 16, 2019 1 hour ago, Jos said: You are still calling working2() inside of func working2() so obviously this could cause issues. What exactly are you expecting to happen there? (also try keeping you script readable by formatting it properly! Tidy can help there. ) expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 While True Working1() WEnd Case $Button2 EndSwitch WEnd Func Working1() MsgBox(0, "", "Step1") Sleep(1000) Working2() MsgBox(0, "", "STEP3") Return EndFunc ;==>Working1 Func Working2() If $counter = 1 Then Sleep(1000) MsgBox(0, "", "STEP2") working3() Return ElseIf $counter = 0 Then $counter = $counter + 1 Working2() ; <=== this is a problem EndIf EndFunc ;==>Working2 Func working3() Sleep(1000) MsgBox(0, "", "Working3") Return EndFunc ;==>working3 Jos ohh so @Jos what should i do to that script to make it infinite ? thanks for advice using Tidy Noted i learn 1 more new thing again hehe Link to comment Share on other sites More sharing options...
Nine Posted December 16, 2019 Share Posted December 16, 2019 Ya your working2 function is deficient too. Not because it will cause an infinite loop, but it does nothing else than calling working3. If the counter = 0 then it calls itself (once) and after counter reach 1 it calls working3. The recursion there is totally useless. So remove the counter and recursion, just call working3... “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...
Bishop12 Posted December 16, 2019 Author Share Posted December 16, 2019 53 minutes ago, Nine said: Ya your working2 function is deficient too. Not because it will cause an infinite loop, but it does nothing else than calling working3. If the counter = 0 then it calls itself (once) and after counter reach 1 it calls working3. The recursion there is totally useless. So remove the counter and recursion, just call working3... @Nine so sir can i call the function like this expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 While True Working1() WEnd Case $Button2 EndSwitch WEnd Func Working1() MsgBox(0, "", "Step1") Sleep(1000) Working2() MsgBox(0, "", "STEP3") Return EndFunc ;==>Working1 Func Working2() If $counter = 1 Then Sleep(1000) MsgBox(0, "", "STEP2") working3() Return ElseIf $counter = 0 Then $counter = $counter + 1 return Working2() ; <=== Test resolve EndIf EndFunc ;==>Working2 Func working3() Sleep(1000) MsgBox(0, "", "Working3") Return EndFunc ;==>working3 Link to comment Share on other sites More sharing options...
jugador Posted December 16, 2019 Share Posted December 16, 2019 (edited) expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Form1", 250, 97, 192, 124) $Button1 = GUICtrlCreateButton("Button1", 0, 0, 100, 57) $Button2 = GUICtrlCreateButton("Button2", 150, 30, 100, 57) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### Global $counter = 0 While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 Working1() EndSwitch WEnd Func Working1() Sleep(100) Working2() EndFunc ;==>Working1 Func Working2() If $counter < 3 Then ;you code... working3($counter) $counter += 1 Working2() Else $counter = 0 MsgBox(0, "", "counter Reset") EndIf EndFunc ;==>Working2 Func working3($oCounter) MsgBox(0, "", $oCounter & ": Working3") EndFunc ;==>working3 Edited December 16, 2019 by jugador 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