Most efficient way to wait before continuing code

How do I pause my code and then play it, when I want it to be play. Here is an example (not using real code):
wait until window2.closed
'do whatever you want once the window is closed

I want to be able to wait until anything, I want happens. That was the best example I could come up with but you could also write that code like this (using REAL code):

'do whatever you want once the window is closed

Thanks in advance, btw.

The easiest and perhaps cleanest way is to use the events already in the framework. In your example, you could place code in the Window2.Close event. That code would not run until Window2 is closing. There are many events in the framework and will likely serve you well in most cases. In other cases, you can trigger your own events based on a set of circumstances that suits your needs.

and then everything else stops until WINDOW2 is closed

from the Lang Ref

You probably want an Observer pattern, in order to be able to react to any event.

To put it another way: the most efficient way to wait is - don’t. Let the event you’re waiting for trigger the next move.

I am not sure if you understood the questions, but thanks anyway. You have taught me something important for my future of programming. Thanks to all.

Can you elaborate on the question, then? A more specific example might help.

I was providing a sample implementation of Tim’s suggestion. I thought we had understood the question, but the answer was “do not try to do it like you are, but set up a process that checks when it should execute instead”

We’re all suggesting essentially the same thing and that is to approach the problem differently. Instead of trying to pause your code, place your code in places that it won’t execute until a set of circumstances is met. Xojo already has many events that only fire under specific conditions. If you need something different, build your own. Tim dove right in and suggested one of the very popular software development design patterns and Eduardo provided a link to a blog post about that pattern. The Observer pattern is probably a good fit, assuming we understand your questions.

If not, perhaps some more details will help us to help you.

Another common design pattern for this is to use a Finite State Machine