pausing yr app - alternatives to thread.SLEEP

Hi
I need to find a way to pause for milisceonds but not using thread.sleep
what options are there ?
I want to make labels fly around the window at a “certain speed” and I thougt of something like this

for i=x1 to x2 labe.left=x1 SLEEP n miliseconds next

I tried using threads but threads cannot acces controls otherwise they will raise an ecception.

Any ideas ?

Have you considered using a timer? It will keep your app responsive to the user and OS so they do not think your app has crashed.

the “animation” takes less than a second. I need something else

Careful, this freezes processing while it execute. Avoid it for long periods.

Sub Sleep(Mil as integer) dim old as double = microseconds while microseconds-old <Mil*1000 wend return End Sub

You can set the period of a timer with millisecond precision (1/1000th of a second). It just defaults to 1 second when you first drag it onto a window in the IDE.

OFFTOPIC

On VB6 there s an API called Sleep (miliseconds) but I noticed that the “speed” depended mostly on CPU power rather than real miliseconds…

[quote=121357:@Horacio Vilches]OFFTOPIC

On VB6 there s an API called Sleep (miliseconds) but I noticed that the “speed” depended mostly on CPU power rather than real miliseconds…[/quote]
That is what Michel’s function does, but it should be avoided if possible in Xojo to prevent the UI from freezing.

[quote=121357:@Horacio Vilches]OFFTOPIC

On VB6 there s an API called Sleep (miliseconds) but I noticed that the “speed” depended mostly on CPU power rather than real miliseconds…[/quote]

Sleep as VB6 does or as the method I posted for you both do is typically procedural. It does not give a chance to the application multitasking to take place. While it executes it holds everything. So it is easy to use and understand, but chances are it will make your application unresponsive if your application lasts too long.

You may want instead to place your animation within a timer, and set the timer period to regulate speed.

Better :

Sub Sleep(Mil as integer) dim old as double = microseconds for i as integer = 1 to Mil*1000 App.YieldToNextThread self.refresh if microseconds-old >= Mil*1000 then exit next return End Sub

This one does not freeze the interface.

[code]Class Window1 Inherits Window

Property Rnd As Random

Sub Open()
Rnd = New Random()
End Sub

// Label1
Sub Open()
Me.Text = CurrentMethodName
End Sub

// Timer1, drag from the Library onto the window and set the following properties in the IDE
// - Mode = Multiple
// - Period = 1500 (milliseconds)
Sub Action()
Label1.Left = Rnd.InRange(0, Width)
Label1.Top = Rnd.InRange(0, Height)
End Sub

End Class[/code]

[quote=121401:@Eli Ott][code]Class Window1 Inherits Window

Property Rnd As Random

Sub Open()
Rnd = New Random()
End Sub

// Label1
Sub Open()
Me.Text = CurrentMethodName
End Sub

// Timer1, drag from the Library onto the window and set the following properties in the IDE
// - Mode = Multiple
// - Period = 1500 (milliseconds)
Sub Action()
Label1.Left = Rnd.InRange(0, Width)
Label1.Top = Rnd.InRange(0, Height)
End Sub

End Class[/code][/quote]

Eli, I am sorry, but I do not understand how you obtain the equivalent of app.SleepCurrentThread(1000)