Sign in to follow this  
Followers 0
element72

Several functions. A couple questions

14 posts in this topic

#1 ·  Posted (edited)

If I have a function with a loop running, then I execute another loop function in the same script, will the current loop function terminate? Then run the 2nd loop function that I want?

Edited by element72

Share this post


Link to post
Share on other sites



If you have 2 functions that are loops the first one will pause after a call to a 2nd loop Function.   Unless you tell it to exitloop it should return to the first.  

Share this post


Link to post
Share on other sites
35 minutes ago, markyrocks said:

Unless you tell it to exitloop it should return to the first.  

Unless I told the 1st loop to exit earlier it should return to the first? is that what you mean? I just need clarification.

Share this post


Link to post
Share on other sites

Are the loops nested?

Share this post


Link to post
Share on other sites
1 hour ago, element72 said:

If I have a function with a loop running, then I execute another loop function in the same script, will the current loop function terminate? Then run the 2nd loop function that I want?

While 1
    While 1
        Sleep(1000)
    WEnd
    ;;Anything here will never happen
WEnd
While 1
    While 1
        Sleep(1000)
        ExitLoop 1
    WEnd
    ;;This will happen after the inside While loop reaches 'ExitLoop 1'
WEnd

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

try this example

for $x=1 to 100

$x+=1
if $x=50 then
_func()
endif
next
msgbox("","","first loop done x=" & $x)

func _func()
for $a=1 to 100
$a+=1

next
msgbox("","","_func() done, a=" & $a)
endfunc

the above should start first loop,  then go to second, finish the second, return to first then finish first (untested)

for $x=1 to 100

$x+=1
if $x=50 then
_func()
exitloop
endif


next
msgbox("","","first loop done x=" & $x)
func _func()
for $a=1 to 100
$a+=1

next
msgbox("","","_func() done, a=" & $a)
endfunc

the above should start first loop, go to the second finish the second, then basically go back to first, read the exitloop, then exit with $x=50 (untested)

 

 

Edited by markyrocks

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

1 hour ago, Dgameman1 said:
While 1
    While 1
        Sleep(1000)
    WEnd
    ;;Anything here will never happen
WEnd
While 1
    While 1
        Sleep(1000)
        ExitLoop 1
    WEnd
    ;;This will happen after the inside While loop reaches 'ExitLoop 1'
WEnd

 

In your second example the program would enter the first while then enter the second while loop, sleep, exit the second loop then do ;whatever at the end of the  first  loop then go back to the top.  Enter first loop again then enter second loop sleep, exit ect again and again.  It's not going to permanently exclude the second loop it will just enter  sleepand exitloop  do whatever at the end of the first loop over  and over 

 

$a=false    ;nothing happened yet so its false

While 1
    While $a<>true
    
    
        Sleep(1000)
       exitloop
    WEnd
    $a=true   ;it went through once now second loop is excluded but it will continue to loop the first loop
WEnd

 

Edited by markyrocks
1 person likes this

Share this post


Link to post
Share on other sites
HotKeySet("{F1}", "Function1")
HotKeySet("{F2}", "Function2")
HotKeySet("{Esc}", "Close")

Function1()

Func Function1()
    While (True)
        ToolTip("Function1 is running")
        Sleep(100)
    WEnd
EndFunc

Func Function2()
    While (True)
        ToolTip("Function2 is running")
        Sleep(100)
        If (Not Mod(@Sec, 10)) Then ExitLoop    ; Every 10 seconds, exit this loop
    WEnd
EndFunc

Func Close()
    Exit 0
EndFunc

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

can you run 2 loops at once? or is that not possible in autoit? I ask this because I know people who have macros and they are still spamming a key on their peripheral, which I believe creates several loops at once right?   These people I'm referring to are using razor synapse, i believe.

Edited by element72

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

On 4/2/2016 at 5:28 PM, AutoBert said:

Are the loops nested?

 no, not nested as the post right after you shows 2 nested loops.

Edited by element72

Share this post


Link to post
Share on other sites

InunoTaishou is relevant to my first post. Sorry about all the confusion. Everyone thought i had a nested loop.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

cou can't run 2 loops at same time, but you can simulate multiple loops at same time:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("guioneventmode", 1)

$gui = GUICreate("KLeines 1 X 1 (OnEvent-Modus)", 400, 120)
$Start = GUICtrlCreateButton("&Start", 10, 10)
GUICtrlSetOnEvent(-1, 'Looping')
$PauseResume = GUICtrlCreateButton("&Pause", 10, 40)
GUICtrlSetOnEvent(-1, 'PauseResume')
GUICtrlSetState(-1, $GUI_DISABLE)
$Anzeige = GUICtrlCreateLabel("Anzeige", 10, 90, 180)
$Inc = GUICtrlCreateLabel("0", 200, 90, 70)
$Timer = GUICtrlCreateLabel('Zeit: ', 270, 90, 70)
GUISetOnEvent($GUI_EVENT_CLOSE, 'MyExit')
GUISetState()
$dtStart = TimerInit()
$bLooping = False

AdlibRegister('Timer', 1000)

While 1
    Sleep(100)
    if $bLooping then GUICtrlSetData($Inc,GUICtrlRead($Inc)+1)
WEnd

Func MyExit()
    Exit
EndFunc   ;==>MyExit

Func PauseResume()
    If GUICtrlRead($PauseResume) = "&Pause" Then
        GUICtrlSetData($PauseResume, "&Weiter")
        AdlibUnRegister('NextPiece')
        $bLooping = False   ;stops executing func _Inc in main loop
    Else
        Looping()
    EndIf
EndFunc   ;==>PauseResume

Func Looping()
    GUICtrlSetState($Start, $GUI_DISABLE)
    GUICtrlSetData($PauseResume, "&Pause")
    GUICtrlSetState($PauseResume, $GUI_ENABLE)
    AdlibRegister('NextPiece')
    GUICtrlSetData($Inc,0)
    $bLooping = True    ;starts executing func _Inc in main loop
EndFunc   ;==>Looping

Func NextPiece()
    ;executing a small piece of a pseudoloop
    Local Static $i = 1
    Local Static $j = 1
    GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
    If $i = 11 Then $i = 1
    $j += 1
    If $j >= 11 Then
        $i += 1
        If $i >= 11 Then
            AdlibUnRegister('NextPiece')
            GUICtrlSetState($PauseResume, $GUI_DISABLE)
            GUICtrlSetState($Start, $GUI_ENABLE)
            $bLooping = False   ;stops execzting func _Inc in main loop
        Else
            $j = 1
        EndIf
    EndIf
EndFunc   ;==>NextPiece

Func Timer()
    Local $Time = TimerDiff($dtStart) / 1000
    Local $HOUR = Int($Time / 3600)
    Local $MIN = Int(($Time - $HOUR * 3600) / 60)
    Local $SEC = $Time - $HOUR * 3600 - $MIN * 60
    GUICtrlSetData($Timer, StringFormat("%02i:%02i:%02i", $HOUR, $MIN, $SEC))
EndFunc   ;==>Timer

 

Edited by AutoBert

Share this post


Link to post
Share on other sites
13 hours ago, AutoBert said:

cou can't run 2 loops at same time, but you can simulate multiple loops at same time:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("guioneventmode", 1)

$gui = GUICreate("KLeines 1 X 1 (OnEvent-Modus)", 400, 120)
$Start = GUICtrlCreateButton("&Start", 10, 10)
GUICtrlSetOnEvent(-1, 'Looping')
$PauseResume = GUICtrlCreateButton("&Pause", 10, 40)
GUICtrlSetOnEvent(-1, 'PauseResume')
GUICtrlSetState(-1, $GUI_DISABLE)
$Anzeige = GUICtrlCreateLabel("Anzeige", 10, 90, 180)
$Inc = GUICtrlCreateLabel("0", 200, 90, 70)
$Timer = GUICtrlCreateLabel('Zeit: ', 270, 90, 70)
GUISetOnEvent($GUI_EVENT_CLOSE, 'MyExit')
GUISetState()
$dtStart = TimerInit()
$bLooping = False

AdlibRegister('Timer', 1000)

While 1
    Sleep(100)
    if $bLooping then GUICtrlSetData($Inc,GUICtrlRead($Inc)+1)
WEnd

Func MyExit()
    Exit
EndFunc   ;==>MyExit

Func PauseResume()
    If GUICtrlRead($PauseResume) = "&Pause" Then
        GUICtrlSetData($PauseResume, "&Weiter")
        AdlibUnRegister('NextPiece')
        $bLooping = False   ;stops executing func _Inc in main loop
    Else
        Looping()
    EndIf
EndFunc   ;==>PauseResume

Func Looping()
    GUICtrlSetState($Start, $GUI_DISABLE)
    GUICtrlSetData($PauseResume, "&Pause")
    GUICtrlSetState($PauseResume, $GUI_ENABLE)
    AdlibRegister('NextPiece')
    GUICtrlSetData($Inc,0)
    $bLooping = True    ;starts executing func _Inc in main loop
EndFunc   ;==>Looping

Func NextPiece()
    ;executing a small piece of a pseudoloop
    Local Static $i = 1
    Local Static $j = 1
    GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
    If $i = 11 Then $i = 1
    $j += 1
    If $j >= 11 Then
        $i += 1
        If $i >= 11 Then
            AdlibUnRegister('NextPiece')
            GUICtrlSetState($PauseResume, $GUI_DISABLE)
            GUICtrlSetState($Start, $GUI_ENABLE)
            $bLooping = False   ;stops execzting func _Inc in main loop
        Else
            $j = 1
        EndIf
    EndIf
EndFunc   ;==>NextPiece

Func Timer()
    Local $Time = TimerDiff($dtStart) / 1000
    Local $HOUR = Int($Time / 3600)
    Local $MIN = Int(($Time - $HOUR * 3600) / 60)
    Local $SEC = $Time - $HOUR * 3600 - $MIN * 60
    GUICtrlSetData($Timer, StringFormat("%02i:%02i:%02i", $HOUR, $MIN, $SEC))
EndFunc   ;==>Timer

 

I don't fully understand this script. can you just briefly explain how it is simulating multiple loops?

Share this post


Link to post
Share on other sites

run it and you see it's simulating 3 loops (repeated actions):

  1. func timer run's until Gui closed, showing the time script is running. This func, using Adlibregister and is executed every second
  2. in mainloop there is the second repeated action, only executed when $bLooping is true.
  3. Clicking the start button is initiating the 3. repeated action which normaly coded with:
    For $i = 1 To 10
        For $j = 1 To 10
            GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
            Sleep(500)
        Next
    Next

    but doing it the normal way the events for Button ($PauseResume) and the the event  $GUI_EVENT_CLOSE are cached in a pipeline (@Dev's or MVP's please correct when wrong) and the functions they are calling are started after the loop has ended (10*10*500 = 50000 msec max.). Breaking down this nested loops to small piece is a solution for getting all events in livetime and simulating parallel executing of repeated actions. Therefore func Looping:

    Func Looping()
        GUICtrlSetState($Start, $GUI_DISABLE)
        GUICtrlSetData($PauseResume, "&Pause")
        GUICtrlSetState($PauseResume, $GUI_ENABLE)
        AdlibRegister('NextPiece')
        GUICtrlSetData($Inc,0)
        $bLooping = True    ;starts executing func _Inc in main loop
    EndFunc   ;==>Looping

    does the part which must done once, disabling Start button, enabling $Pause Resume button etc. and register the func  func NextPiece. Also the boolean $bLooping is set to true, so the label $Inc is every 10 ms updated.
    The func NextPiece:

    Func NextPiece()
        ;executing a small piece of a pseudoloop
        Local Static $i = 1
        Local Static $j = 1
        GUICtrlSetData($Anzeige, $j & " * " & $i & " = " & $i * $j)
        If $i = 11 Then $i = 1
        $j += 1
        If $j >= 11 Then
            $i += 1
            If $i >= 11 Then
                AdlibUnRegister('NextPiece')
                GUICtrlSetState($PauseResume, $GUI_DISABLE)
                GUICtrlSetState($Start, $GUI_ENABLE)
                $bLooping = False   ;stops execzting func _Inc in main loop
            Else
                $j = 1
            EndIf
        EndIf
    EndFunc   ;==>NextPiece


    first register herself and does a small peace of the whole job incrementing the values for mulitiplication and updating the label $Anzeige. The func is also checking is whole work done (showing 1*1, 1*2, ... 1*10,...10*10), if work is done the it's unregsitering herself and setting the buttons state.
    The func PauseResume:

    Func PauseResume()
        If GUICtrlRead($PauseResume) = "&Pause" Then
            GUICtrlSetData($PauseResume, "&Resume")
            AdlibUnRegister('NextPiece')
            $bLooping = False   ;stops executing func _Inc in main loop
        Else
            GUICtrlSetData($PauseResume, "&Pause")  ;<====== new inserted
            Looping()
        EndIf
    EndFunc   ;==>PauseResume

    toggles state of $PauseResume button.
    If state (Text of Button) is  '&Pause' unregister func NextPiece, setting the new state (Text of Button) to resume.
    If state (Text of Button) is  '&Resume' setting the new state (Text of Button) to pause and  starting func looping.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0