Sleeping execution

Me: Xojo 2021v3, M1 mac mini, MacOS 15.2

My app draws stuff to a canvas: (called from the canvas Paint(g) event)
for i = 0 to 100
g.DrawLine(2i, 5i, 10, 20)
next

This works fine. What happens is that the result of all the drawing appears instantly. I assume this is because the computer is really fast. Maybe some other reason for appearing instantly (buffering?)

What I want to have happen is to pause execution between the drawing the items:
for i = 0 to 100
g.DrawLine(2i, 5i, 10, 20)
SleepCurrentThread(1000)
next

But this seems to get lost. (looping?)

Is there any way to accomplish the goal of seeing items being drawn to a canvas individually, with a short pause between their appearances?

Is there some problem with
self.Show, calling this loop from a dialog box, etc?

Are you doing this in a thread? If not, I’m not sure what the SleepCurrentThread will do.

If it’s not in a thread, suggest you put it in one. You won’t be able to put the g.Drawline in the thread through, so your logic will get harder.

You should read the series of articles I wrote on animation in Xojo. It’s best to use a timer and draw in steps. There are other ways to accomplish this, but you’re bound to run in to other issues.

Animating Xojo, Part 1
Animating Xojo, Part 2
Animating Xojo, Part 3
Animating Xojo, Part 4

you could sub class a canvas, add a property for your iteration.
invalidate (EraseBackground =False) this canvas by a timer
draw a line at paint event with the help of your property (location)
change your property as example Me.Y=Me.Y+1

Your text implies to me that you have a wrong concept of a paint event. That is being called whenever it is necessary to refresh a control, so everything it should be used for is to (re)create the looks of the control. Its content will be shown when it is finished but not while drawing. If a paint event takes long to finish, the result will be a bad screen refresh rate but not an animation.

A Canvas’ Graphics object is not an image buffer. Or rather a volatile one which will be cleared when the next screen refresh is started.
With that in mind, Anthony’s series should give you all information necessary for your task.

1 Like