Fighting the endless loop

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Mitch
Newbie
Posts: 5
Joined: Wed Apr 22, 2020 9:13 pm

Fighting the endless loop

Post by Mitch » Tue May 12, 2020 3:34 pm

Is there any method to stop a script (automatically) if it runs for too long (a pre-determined amount of time)? Or to start a 2nd script if the first script runs for too long?

As my scripts get more complicated, I randomly have one end up in an endless loop. Our fix to the endless loop is always the same, stop the script, close all the open windows and start the script again but sometimes it is hours or days before someone notices that system isn't working.

Obviously I'm trying to modify the scripts to stop the issue from happening again but they keep showing up in parts of a script that ran without issue for months.

If what I'm requesting doesn't exist but you have another suggestion, please don't hesitate to chime in.

Thank you everyone

Mitch

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 628
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Fighting the endless loop

Post by Dorian (MJT support) » Tue May 12, 2020 5:00 pm

If it is actually looping you could try counting how many times it's looping, and exit if it reaches X times. If

Code: Select all

Let>loopcount=1
..beginning of pseudo loop

if>loopcount>10
  exit / or call another macro
Endif

..end of pseudo loop
If it's time based, you can do the same thing based on elapsed seconds.

Code: Select all

Timer>startTime
..beginning of pseudo loop


..
.. do something here
..
Timer>endTime
Let>elapsed_seconds={(%endTime%-%startTime%)/1000}
If>elapsed_seconds>600
 exit / call another macro
Endif
..end of pseudo loop
Yes, we have a Custom Scripting Service. Message me or go here

Mitch
Newbie
Posts: 5
Joined: Wed Apr 22, 2020 9:13 pm

Re: Fighting the endless loop

Post by Mitch » Tue May 12, 2020 6:01 pm

My issues is time based but correct me if I'm wrong but if the script is stuck in an endless loop, then wouldn't the following code not get an opportunity to run?

Timer>endTime
Let>elapsed_seconds={(%endTime%-%startTime%)/1000}
If>elapsed_seconds>600
exit / call another macro
Endif

Mitch
Newbie
Posts: 5
Joined: Wed Apr 22, 2020 9:13 pm

Re: Fighting the endless loop

Post by Mitch » Tue May 12, 2020 6:02 pm

And forgive me for not saying it already but thank you for taking the time to help. I really do appreciate it.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 628
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Fighting the endless loop

Post by Dorian (MJT support) » Wed May 13, 2020 10:47 am

That's the question - if it is in an actual loop, you can use methods similar to mine to exit. If it isn't an actual loop and is simply stuck on a line of code, then it's best we find out why. Do you have any WaitWindowOpen lines that may be stalling it? That's quite a common occurrence and can often be resolved by making sure to use a wildcard. For example using WaitWindowOpen>MyWindow* as opposed to WaitWindowOpen>MyWindow12345.

All that aside, if we simply want to abort after X minutes, I put the following together for you.

It should abort the script even if it's "stuck", after %StopMins% minutes have elapsed. It gets the current time, add StopMins minutes to that, figures out if it needs to adjust the time (for example we want 1159 to become 1204 and not 1164), and then writes to a logfile and aborts if StopMins is exceeded.

Code: Select all

//How many minutes to stop after?
Let>StopMins=5

//Get the current time
hour>hh
Min>mm

//For testing
//let>hh=11
//let>mm=59

//Add %StopNum% minutes to hhmm, adding to the hour if we need to.
if>mm>{60-%StopMins%-1}
  let>mm={%mm%-(60-%StopMins%)}
  let>hh=hh+1
  else
  let>mm=mm+StopMins
endif

//Format hh and mm to 2 digits
Format>%.2d,hh,hh
Format>%.2d,mm,mm

//For testing
//mdl>%hh%%mm%

//Make sure this is above your current code
Onevent>TIME,%hh%%mm%,,abort

//The body of your script.
Label>AllMyCode
Wait>1
Goto>AllMyCode

//Abort Routine
srt>abort
  //Change the file path
  DateStamp>d:\AbortLog.txt,%script_name% Aborted
  exit
END>abort
Yes, we have a Custom Scripting Service. Message me or go here

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts