Sign in to follow this  
Followers 0
Wb-FreeKill

Func in loop - execute every second pass

6 posts in this topic

I dont know how to explain this, but i have a TCPAccept func, actually several in a loop with other stuff. But the TCPAccept func i very slow sometimes, especaly when there is several in the loop. So i would like to have the func execute every second or third time is passes in the loop, because it dosn't have to accept that often, only every 1 sec or something... is that possible to do in a loop?

thx for any help!

Share this post


Link to post
Share on other sites



This is one way you could do what you want

Initialize counter outside of loop

Begin loop

Increment counter

Is counter = 3

Yes = Execute function + reset counter

No = continue loop

Next

Share this post


Link to post
Share on other sites

If you are using a For loop, you can use something like the following:

$factor = 3
For $i = 1 to 10
  If Int(($i-1)/$factor) = ($i-1)/$factor Then
    If MsgBox(1, "Test", "Here is where you put your function." _
      & @LF & " $i = " & $i & ".") = 2 Then Exit
  EndIf
Next

Even though the loop runs 10 times, you should see the Msgbox only 4 times. The ($i-1) is used to force the Msgbox to show the first time through the loop.

BlueBearr


BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

im using a while, but the counter has to be very large, i mean - the loop is looping many times a sec right?

So i could do something like this?

#Include <GUIConstants.au3>


GUICreate("GUI",200,200)
$Label = GUICtrlCreateLabel("",30,30,60,20)

GUISetstate()

$Counter = 0
While 1
Sleep(20)

    $Counter = $Counter +1
    GUICtrlSetdata($Label,$Counter)
    If $Counter = 100 Then  
        _Execute()
        $Counter = 0
    Else 
        ContinueLoop
    EndIf   
WEnd




Func _Execute()
Msgbox(0,"Test","This is a test")
EndFunc

Share this post


Link to post
Share on other sites

You may want to consider using a timer based increment. This might give you more of the control that you are seeking, and it will be less dependent on individual system speed:

#Include <GUIConstants.au3>
GUICreate("GUI",200,200)
$Label = GUICtrlCreateLabel("",30,30,60,20)

GUISetstate()

$Counter = TimerInit()
While 1
Sleep(20)

    GUICtrlSetdata($Label,Round(TimerDiff($Counter),-1))
    If TimerDiff($Counter) >= 5000 Then ; 5 seconds
        _Execute()
        $Counter = TimerInit()
    Else 
        ContinueLoop
    EndIf   
WEnd
Func _Execute()
Msgbox(0,"Test","This is a test")
EndFunc

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

That code will work fine -- it is more common to use the Mod function for this sort of control however. Mod evaluates $counter/$increment and returns the remainder -- 0 means it divided evenly:

#Include <GUIConstants.au3>

GUICreate("GUI",200,200)
$Label = GUICtrlCreateLabel("",30,30,60,20)

GUISetstate()

$Increment = 100; Execute once in every $Increment times
$Counter = 0

While 1
Sleep(20)

    $Counter = $Counter +1
    GUICtrlSetdata($Label,$Counter)
    If Mod($Counter, $Increment) = 0 Then  
        _Execute()
    Else 
        ContinueLoop
    EndIf   
WEnd

Func _Execute()
Msgbox(0,"Test","This is a test")
If $Counter = 300 Then Exit 0
EndFunc

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

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