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.
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.