How to create an endless loop you can cancel

Touch Portal How to Guides

This guide will show you how to create a loop in Touch Portal that will run endlessly but which you can stop if you want to.

Step 01: Create a button

We start by creating a button and add the title "Start Loop". This title indicates the start state of this button.



Step 02: Add a Repeat X times action

We need to use the Repeat X times action for our loop. The Repeat action will execute the flow of actions attached to it the given amount of times. So if you put a value of 5 in the repeat action it will do the flow of actions 5 times.

Add the action and give it a value of 2147483647, which is the highest possible value and which will be more enough to be used as endless. If each iteration takes 25ms it will take 2 years or so to reach the end.

Step 03: Add an action

So in an endless loop we want to do some actions of course. In our example we are going to play a beep sound. Add a Play Audio action and point it to a sound you have.

Step 04: Timing is important

So if we run this it will play the beep sound endlessly in one go so do not start it yet! Touch Portal does not wait for the sound to be done with playing. So this will sound like a machine gun at the moment. Time to fix this.

Please note that if you did run this already it will run endlessly and can only be stopped at this point by fully closing Touch Portal using the icon in the system tray, right clicking it and choose "Exit" to close TP.

Our beep sounds duration is 1 second but we also want to silence after it so we will add a Wait for Timer action that will wait for 2 seconds. If you would run this it would now play a beep sound each 2 seconds. But it is still unstoppable. Once this starts runnning it will keep on running endlessly.



Step 05: Add the Stop Logic

This is the final step of our endless cancelable loop. So the first thing we need to do when the button is pressed is to Toggle the button state. This way we can turn on the loop when we press it and we can stop it when we press it again.

We will only start the endless timer when the button is turned on when we press it. To do this I have wrapped the whole Repeat action in one If Statement which will only trigger if the button is on. When we press it again and the toggle action will turn it off again.

Within the Repeat function we now have added the escape logic. If the loop is running it will check each iteration if the button state is off. If it is, it will execute the Stop executing actions in current flow and that will stop all actions from executing within this flow. Because we have made the exit logic part of the running flow it will work as expected.

Please note that if you would have placed the Stop executing action in current flow action in an else for the outer If Statement it would not have worked because in that case it would not have been in the same flow. Each time you press the button a new independent flow is created and started.

End Result

You now have a button which holds an infinite loop playing a beep each 2 seconds that you can cancel running at any given moment you desire.


Back to Guide Overview