Repeat something - Use Ticks or Timer

I want to press a button that starts a calculation every 2 seconds and displays the new result in the same textfield (and eventually show a progressive graph) and ends after 40 seconds (20 iterations).

Whats the best way - ticks or timer?
Thanks

Timer.
…Which either increments a form or global level counter, or a static variable.
Then stops when the upper limit is reached

Apart for @Jeff_Tullin’s answer, I don’t know what “ticks” means in this context.

What code would you use with the Timer to automatically increment it. I’m trying it in multiple mode and it moves the screen position (as a test), but within that if-then loop it won’t increment a global variable. It stays the same.

You have a timer set to multiple

Inside that you add 1 to a global variable every time

if gv = 20 then
me.mode = 0 //stop the clock
else
gv = gv+1
'do stuff
end if

or a static variable

static sv as integer
       if sv = 20 then
        me.mode = 0 //stop the clock
        else
        sv = sv+1
    'do stuff
        end if

I know what the Ticks function does, I just don’t know what you mean when you offer it as an alternative to a Timer. What would that code look like?

Don’t know. Just trying to see if it would work to solve my problem.
I think I have it solved with the timer,
Thanks

Timer is the right way to do this. If you want to check the ticks you would need an endless loop for doing so, but endless loops will freeze your use interface.

So as said above you actually need a timer firing at a certain interval, in your case 2 seconds. And as this timer is firing at a certain interval which you have to define anyway, you don’t need to use that timer to check the ticks for instance, just run your code ;-).

Dim Count As Integer
Do
If ticks > lastticks + 20s
Count = Count +1
lastticks = ticks
DoCalcAndShowResult
DoEvents
End If
If Count = 20 Exit
Loop

:woozy_face:

(Don’t use Ticks)

Let’s hope Gary will not mark your code as solution ;-), At least it would be a lot of fun for the next generation of Xojo developers :wink:

see also
Timer.CallLater(2000, AddressOf MathMethod)

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.