handle infinite loops

How can I handle infinite loops in client actions? During infinite loops, my application freezes. Instead I want in to to throw an error message saying infinite loop detected. How can we achieve this?

mvp_badge
MVP
Solution

Hello Krishnnambal,

Hope you're doing well.

It is not quite normal to have infinite loops in your application. I mean, it is not a desirable thing, it is usually associated to a bug and it should be fixed and avoided.

Can you explain to us what exactly is your use case?


Anyway, you can define a Counter local variable that will count your cycle iterations. The Counter will start at 0 and you should define a large number of maximum iterations that you will assume that you have an infinite loop when you reach it. As an example, let's assume that 100.000 iterations is the threshold.

So you will need to check during the cycle if the Counter reached your threshold (in this case 100.000). If so, you throw an error message. If not, you execute one iteration and increment your Counter. Something like this:


There's obviously some concerns about this that I would like to share:

1) You may be hiding an infinite loop which, in my opinion, should be fixed without recurring to this;

2) You'll need to define a threshold and it's always a risk. If the threshold value is too short, it is a risk for longer loops that may not end. If the threshold value is too large, you'll have to wait a lot until you detect that you have an infinite loop and the user will need to wait during that time.


Another thing that you may use is the Maximum Iterations property of the For Each widget:


If you define a value for that property, you will make sure that your cycle will end (when it reaches the maximum iterations defined). It's just another way to prevent the infinite loop to freeze your application.


Hope that this helps you!


Kind regards,

Rui Barradas

Thanks, Ruii

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.