Command to break while loop if stuck in a script

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.