Command to break while loop if stuck in a script

A way to break out of a while loop would be nice. Several times while testing a while loop inside a script, the script got stuck in an infinite loop. The only way I found to get out of it is to “End Task”, relaunch Designer, etc.

I don’t know of any command, I add the following code when I am testing new For / While loops.

EC = 0 #Error Checker
while (your statement here) or EC < 21:
	EC += 1
	Other code here.

if EC > 20:
	print EC

After testing is complete you, you can email yourself with so you can track errors.
Or remove the Error code.


There is a break command for a while loop. You can read about it here…

The error checking code is good practice I agree and I am aware of the break command. What happens if you inadvertently get stuck in an infinite loop? For example, MatLAB uses Ctrl+C or Ctrl+Break to stop execution.

I have had a situation similar to this in the past while writing C# code. What I did there was set a variable to the number of milliseconds, seconds, or whatever that I will allow the loop to run, then grab the date and time into another variable when I enter the loop. The while statement would check whatever it normally checks for in the loop, and also subtract the date and time stored in the time entered variable from the current date and time. If the difference exceeds the value of the variable set with the maximum time the loop is allowed to run, the while would bust out. I used an AND of the normal loop check and the time lapse check so either one would bust out.

That doesn’t sound very explanatory, so I’ll do it with pseudo code…

set timespan type variable maxLoopTime = 5
set datetime type variable loopStartTime = the current datetime

while ((whatever I check for is in limits) AND (the current datetime - loopStartTime < maxLoopTime))
do whatever

This doesn’t require the break command, which some programmers consider bad coding if you use it. Everything is checked in the while statement, and it falls out of the loop when either comparison is false.

Some would argue that this is bad coding as your main comparison should not even take place unless the data being compared is good quality and will always work. I tend to agree with this, but in the world of OPC, tags can go bad quality after you enter a while loop, causing it to hang up in the loop. So a solution like this will at least break it out and let the rest of your world continue to revolve.

During development the abilty to stop a continously or sluggish script would be a nice feature. I have also gotten myself locked out a few times to my annoyance.

I agree the iteration counter limit from CPowell is the most expedient for most cases.

The problem you still have is if code hangs within an iteration - eg use of a blocking function…then both methods will fail to reach the while test and you are still stuck.

As a result graceful handling of inf loop condition would be useful.

1 Like

I don’t care how good you are, one > instead of < and now you’re heading for a stack overflow. This is a necessary feature.

I’ve requested an interrupt, if everyone upvotes it maybe it’ll happen: