Implementing a Do While Loop in Power Apps Using a Timer Control

Introduction

In traditional programming languages like C# and Java, the Do While loop is a common structure that allows code to run repeatedly until a specific condition is met.

Power Apps does not natively support this loop construct. However, with a bit of creativity, you can replicate the same behavior using a Timer control.

This article explains how to build a Do While loop in Power Apps step by step.

Why Power Apps Needs a Workaround

In C#, a Do While loop looks like this:

do { 
// code to execute 
} while (condition);

Power Apps does not provide this loop structure directly.

Instead, you can simulate it by combining:

  • Button control (to start the loop)
  • Timer control (to repeat actions)
  • Variables (to track state and conditions)

Step-by-Step Implementation

1. Create a Start Button

  • Insert a Button and name it Start.
  • In its OnSelect property, initialize two local variables:
UpdateContext({ runTimer: true, count: 0 })

2. Configure the Timer Control

  • Add a Timer control.
  • Set its Start property to: runTimer (this means the timer will start when Start button is clicked because this variable will be set to true)
  • Set Repeat to true.
  • Adjust Duration to 2000 (2 seconds) for demonstration. (In real scenarios, you can use shorter durations like 50 ms for faster execution.)

3. Display the Counter (Optional)

  • Add a Label to show the current value of count.
  • Set its Text property to: count

4. Add Logic in OnTimerEnd

This is where the “Do While” behavior is implemented.

If(
    count < 3,
    UpdateContext({ count: count + 1, runTimer: false });
    UpdateContext({ runTimer: true }),
    UpdateContext({ runTimer: false });
    Notify("Done")
)
  • If count < 3:
    • Increment count.
    • Stop and restart the timer (to simulate looping).
  • Else:
    • Stop the timer.
    • Notify the user that the loop is complete.

Example: Adding Random Numbers to a Collection

Instead of just incrementing a counter, you can perform real operations inside the loop.

  • Create a collection:
Collect(newNumbers, RandBetween(1,100))

  • Collect(newNumbers, RandBetween(1,100))
  • Insert a Gallery and set Items property to newNumbers.
  • Run the app, click Start
  • Each loop iteration adds a random number until the condition is met.

Result: When count reaches 3, three random numbers are added to the collection and displayed in the gallery.

Handling Longer Operations

  • If your loop involves operations like Patch (writing to a data source), execution may take longer than the timer duration.
  • To avoid overlapping processes:
    • Create an additional variable, e.g. isProcessing.
    • Set isProcessing = true when an operation starts.
    • Set isProcessing = false when it completes.
    • Only allow the next iteration if isProcessing = false.

This ensures that external operations finish before the loop continues.

Conclusion

While Power Apps does not natively support the Do While loop, you can replicate its behavior using a Timer control combined with variables. This approach allows you to:

  • Run repeated operations until a condition is met.
  • Perform tasks like updating collections, patching data sources, or sending emails.
  • Control execution speed and prevent overlapping processes.

With this workaround, you unlock a powerful programming pattern inside Power Apps, enabling more dynamic and automated app behaviors.

Comments

Leave a Comment

Your email address will not be published. Required fields are marked *