Making the code pause?

Hi, so I’ve been using Xojo for a day and I’m loving it.

I’ve worked out that using a timer allows me to use pauses efficiently, but is there a way I can pause code in a code not in a timer, for example a TextLabel; I want it to do something like this (pseudocode):

me.text=hi
wait a second
me.text=hello
wait a second
me.text=alright

Is there a way I can do this?
Thank you.

Why would you want to? There’s no reason to make your user wait, that’s poor UX.
Found this in the docs.

// do nothing for 1/4 second or so Dim waitUntil As Integer = Ticks + 15 While ticks < waitUntil Wend

If I didn’t have a wait the user would only see the final text, rather than the hi and the hello too…

[quote]Found this in the docs.

// do nothing for 1/4 second or so Dim waitUntil As Integer = Ticks + 15 While ticks < waitUntil Wend[/quote]
I see thanks, I’ll try it out now. What is ticks?

http://documentation.xojo.com/index.php/Ticks

I see, thanks.
But I still have no idea how to make the code pause for a second?
Another example is if a user enters something wrong I want a message to appear saying Error or something, and I want it removed after a second.
For educational purposes i do not want to use a MessageBox.

You can use a plain box window and a label on it saying whatever is needed. Then use a timer to display it with window.show, and close it one second later with window.close. See modal window if you want it to stop execution while it displays like a MsgBox.

I don’t know what the code would be in a timer for all that, an example would be greatly appreciated.

I used to create games on a game engine in Lua, and in that was a built-in delay function, simply writing

wait(1)

would make the code wait a second.

Is there no way to do this on Xojo?

I say this with much respect. Xojo is not Lua so stop trying to force Xojo to work just like Lua.

You have to do a little work on your own. Read the documentation on Timers, Windows Types, etc. Look at the copious amounts of example code that comes with Xojo. If you feel you need additional training you can subscribe to over 56 hours of video training from me at http://xojo.bkeeney.com/XojoTraining/

This community is usually very kind and helpful. But, you have to at least make an attempt at learning the language on your own. I realize you’re a day into it, but any development language requires you to do a little homework. The language reference and example programs are really a good way to get going.

Have you read the code from Tim Parnell above ? That is the way.

[quote=143234:@Bob Keeney]I say this with much respect. Xojo is not Lua so stop trying to force Xojo to work just like Lua.

You have to do a little work on your own. Read the documentation on Timers, Windows Types, etc. Look at the copious amounts of example code that comes with Xojo. If you feel you need additional training you can subscribe to over 56 hours of video training from me at http://xojo.bkeeney.com/XojoTraining/[/quote]

I do; I’m not someone who just asks for help without trying myself. I try for an hour before asking for help.

I’ll remember that, thank you.

I’ll reinforce what [quote=143150:@Tim Parnell]There’s no reason to make your user wait, that’s poor UX.
Found this in the docs.

// do nothing for 1/4 second or so Dim waitUntil As Integer = Ticks + 15 While ticks < waitUntil Wend[/quote]
While this code will do what you asked (mimic wait(1)), I would reinforce that it really is a poor, outdated approach. In a modern event-driven language like Xojo, a Timer is a much better solution.

[quote=143226:@Rami Hassan]
I used to create games on a game engine in Lua, and in that was a built-in delay function, simply writing

wait(1)

would make the code wait a second.

Is there no way to do this on Xojo?[/quote]

click me

[quote=143149:@Rami Hassan]me.text=hi
wait a second
me.text=hello
wait a second
me.text=alright[/quote]

You can pause execution using App.SleepCurrentThread. While paused your UI is not interactive, you can’t click buttons and I think Timers won’t fire either. It’ll have the same effect as a while loop to pause except it uses less resources.

Also, the UI doesn’t update automatically until code finishes executing, so here, after changing the controls text you need to manually tell it to Refresh.

[code]Sub Action() //Pushbutton event

label1.Text = “hi”
label1.Refresh

app.SleepCurrentThread(1000) //1000 milliseconds = 1 second

label1.Text = “hello”
label1.Refresh

app.SleepCurrentThread(1000)

label1.Text = “alright”

End Sub
[/code]

My contribution!
Maybe someone find it useful!
It’s an awful lot of code to do such a simple thing to write some text…
But in greater projects, it’s more useful.

http://syscare.se/files/thread_and_timer.zip

[quote=143149:@Rami Hassan]Hi, so I’ve been using Xojo for a day and I’m loving it.

I’ve worked out that using a timer allows me to use pauses efficiently, but is there a way I can pause code in a code not in a timer, for example a TextLabel; I want it to do something like this (pseudocode):

me.text=hi
wait a second
me.text=hello
wait a second
me.text=alright

Is there a way I can do this?
Thank you.[/quote]

At this point of the discussion, I feel it is necessary to make the difference between procedural code, and event driven code.

In the past, since the early days of Basica and AppleSoft basic, a program started at line 10 and went on in a procedural way one line after the other. So in essence what you want to do is go one line after the other and wait in between, just like you would have done 30 years ago.

Xojo and other modern languages proceed otherwise. Instead of going one line after the other, they use subprograms which execute in event handlers upon actions taken by the user of the program. Typically, when the user clicks it triggers the execution of the button Action event. There is no notion of wait because intrinsically the program simply does nothing until an event happens.

A timer creates an event at intervals. Instead of waiting one second, it does something every second (or any needed interval). So what you want to do will be carried out in the modern event driven model not as “do this, wait, do that, wait”, and so on, but as “do something every second”.

Your example could be carried out very easily by a timer Action event.

  • Drop a timer on the window
  • Drop a TextArea1
  • In the Timer, put this Action event :

Sub Action() static counter as integer = 1 select case counter case 1 TextArea1.Text = TextArea1.Text + "Hello " case 2 TextArea1.Text = TextArea1.Text + "World."+EndofLine case 3 TextArea1.Text = TextArea1.Text + "How are you ?" end select counter = counter + 1 End Sub

Contrary to the wait() or app.SleepCurrentThread() model, you can do all sorts of things in between the events. Just like in real life, you can go have a beer during commercials on TV. Move things on screen for the user entertainment, do housekeeping in your program, and so on.

I strongly recommend you try forgetting the old procedural model. Everything in a Xojo program is event driven. It will be a lot easier for you to program in Xojo if you embrace this model early on.

Thank you!!
That was neat!

It seems a lot more pleasant than the silly code I posted above.
I’ve updated the example and included your code.

I must have been asking the wrong questions earlier! Because this way seem much more easy and convenient!
Thank you!

Um… not exactly. Xojo is still just as procedural, it just runs an event loop for you. This is what App.DoEvents does, it executes a single iteration of the event loop.

The psuedocode for the loop would look something like:

App.Open While True Try App.DoEvents Catch Err As RuntimeException If App.UnhandledException(Err) Then Continue Else Exit End If End Try Wend App.Close

And the meat of App.DoEvents would be

GetUserInput TriggerUIEvents TestTimers PollSockets For Each Thread In App.Threads Thread.RunUntilYield Next UpdateUI

Everything is procedural. Even the threads and timers. Xojo’s cooperative threads just divide up time to give an appearance of concurrency.

This is why you can’t sleep the main thread, because execution would just stop. That main thread must keep going in order to actually trigger events, like resuming a thread from sleep.

The UI updates at the end of the DoEvents execution is why your app will lock up when you do lots of work on the main thread. It holds up execution at the TriggerUIEvents step.

So because of all this, pausing execution is just the wrong way to go in every sense. It is safe to do from inside a thread, but nothing but trouble on the main thread.

Michael’s solution is correct. Use a timer to check the status of something and begin work when ready.