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 system.net.sendEmail 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))
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.
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: https://inductiveautomation.canny.io/ignition-features-and-ideas/p/stopinterrupt-for-script-designer
The ideas site says that this is complete, but I've never been able to get it to work. Every time I put a while loop in, I somehow manage to get myself stuck in it, and it always takes a force quit and a loss of work to recover.
This morning, I had console open, and I was using an iteration count as a safety to break a while loop, but I commented out a block of code to focus on something that was giving me trouble, and of course, I commented out the iterator.
Has anybody ever successfully used this feature?
Are you talking about the Designer's scripting console, or just the console window to show output from component scripts? If the latter, it isn't expected to work.
Threw this script into the script console, and was able to interrupt it prior to it breaking out.
testCount1 = 0 testCount2 = 1000000000000 while testCount1 < testCount2: testCount1 += 1 if testCount1 > 10000000: break; print testCount1
The Ctrl+C shortcut doesn't seem to work, however, the Execute button becomes an interupt button and that does work.
Interesting that it prints
Keyboard Interrupt when the key board short cut doesn't work.
That explains it; it was the latter. I wasn't working on anything important: just an idea that I was toying around with. I took a screen shot the unsaved portion of my script, and I recovered almost everything with an online image to text converter. Perhaps if I get time, I'll mess up a while loop on purpose in the script console and see if I can get away with it there. I so rarely use that thing that I forget about it.