Xojo is event driven, you should use event to react to something happening instead of waiting for it to happen. The only acceptable way to wait is to use Self.Sleep(1000) inside a thread’s Run event, that will make the thread sleep for 1 second and resume on the next line.
[quote=373210:@Asis Patisahusiwa]Using do…loop
Wrap the code inside a Thread and call App.SleepCurrentThread
However, both solutions will cause high CPU usage.[/quote]
The loop will burn CPU. Not App.SleepCurrentThread.
About XojoScript, why specifically would you need to pause execution ? Even so, what prevents you to have a XojoScript for the beginning of execution, then a second XojoScript that will be launched from a timer Action event ?
As Thom just said, don’t use outdated waiting loops. This is typical of procedural thinking we used back in the eighties when we did not have events. Antiquated.
Try to wrap your head around timers and event programming. Once you have grasped the concept, you will realize how much cleaner and simple it can be.
At any rate, I frankly think even that is not the best way to deal with delays. The real solution in an event driven programming is Timer. Timer does not burn CPU.
This simple XojoScript code demonstrate why I can’t use timer.
dim x as Integer
// how can I cut this out and do a looping inside a timer without resetting x value each time?
while true
x = x + 1
Print str(x)
delay(1000) // delay 1000ms
wend
[quote=373344:@Asis Patisahusiwa]This simple XojoScript code demonstrate why I can’t use timer.
[code]
dim x as Integer
// how can I cut this out and do a looping inside a timer without resetting x value each time?
while true
x = x + 1
Print str(x)
delay(1000) // delay 1000ms
wend
[/code][/quote]
[quote=373344:@Asis Patisahusiwa]This simple XojoScript code demonstrate why I can’t use timer.
[code]
dim x as Integer
// how can I cut this out and do a looping inside a timer without resetting x value each time?
while true
x = x + 1
Print str(x)
delay(1000) // delay 1000ms
wend
[/code][/quote]
So you need your XojoScript to count like a clock?
Yeah, you definitely need to run that inside a thread. Create a thread, set the run event to something like
Do
Script.Execute()
Self.Sleep(1000)
Loop
You should have no problem with that. Don’t use App.SleepCurrentThread if you’re already in a thread, that’s a waste since you already know which thread to sleep. Is it a big waste? No, probably not, but it is still a waste. I don’t think in all my years I’ve ever found a good use for App.SleepCurrentThread. Not saying a use case doesn’t exist, but I am saying its usage should be rare at best.
Don’t run the loop inside your XojoScript though, that’s going to be trouble.
I guess this in a web environment? You could use the example I posted and thread off a class1 for each user of the website, each xojoscript is self contained then.
Indefinite while loops are known to spike cpu usage, I consider it one of the reasons to avoid them. To confirm the behavior, I just did it on my Mac
For this specific example subclassing would do the trick. Store x as a property and print it’s value in the Action event handler. Set the period as desired, and for extra accuracy confirm the time between prints before printing.