Possible to kill macro after set duration? - SOLVED

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
headgeek
Newbie
Posts: 8
Joined: Wed Feb 01, 2017 9:11 pm

Possible to kill macro after set duration? - SOLVED

Post by headgeek » Wed Feb 01, 2017 9:40 pm

I'm new to Macro Scheduler, but have found this to be an incredibly easy to use tool.

I have written a pretty complicated macro that controls a JRE - Java Webstart App. This JRE App is less than perfect. As a result, I can get random hangs & random error windows. If I stop the macro & re-run it, it usually will work just fine. This if fine while I am infront of the computer, but once I put this into production, I need this to be much more bullet-proof.

What's the best way of adding in "If 120 seconds goes by, without progressing to the next line, kill macro & restart"? I've looked around for several hours & haven't found exactly what I am looking for (don't know if I need to look more into doing something with OnEvent or look more into doing something around the Log Files).

Thank you in advance for any help you can offer.
-Joe
Last edited by headgeek on Tue Feb 07, 2017 3:09 pm, edited 1 time in total.

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Possible to kill macro after set duration?

Post by JRL » Thu Feb 02, 2017 2:25 pm

I posted a possible solution HERE a couple of months ago. Of course its generic and would need to be modified to suit your needs. Fundamentally you'd throw out the Loop portion of my script, reset the first line to whatever time you want the timeout to be (in seconds) and place the line Timer>StartTime at appropriate places in your script. The Timer>StartTime line will reset the timer to zero. If the script does not reach a line where the timer is reset to zero in less than timeout seconds, the ResultEvent subroutine is started. You would change the ResultEvent to code that will close and restart your script. If you need help with that let us know.

So your script would look something like:

Code: Select all

Let>TimePeriod=120
Timer>StartTime
OnEvent>Custom,EventTimer,TimedOut,ResultEvent

SRT>EventTimer
  Timer>EndTime
  Let>TotalTime={(%EndTime%-%StartTime%)/1000}
  If>%TotalTime%>%TimePeriod%
    Let>TimedOut=True
  EndIf
END>EventTimer

SRT>ResultEvent
  //Start process that will wait for this script to exit
  //then restart this script
  
  //Do any closing processes needed before exit
  
  Exit>0
END>ResultEvent


//Stuff in your current script
Timer>StartTime

//Stuff in your current script
Timer>StartTime

//Stuff in your current script
Timer>StartTime

//Stuff in your current script
Timer>StartTime

//Etc

headgeek
Newbie
Posts: 8
Joined: Wed Feb 01, 2017 9:11 pm

Re: Possible to kill macro after set duration?

Post by headgeek » Mon Feb 06, 2017 8:07 pm

Thank you for you help! This is a great way to solve this problem!

I have added in some additional instructions, that will take a screenshot of where the computer was when it timed out, and then email it out. I got this working, but for some reason, it does it twice. Any idea of what would cause this?

My initial impression is that the script times out and
1.jumps into the subroutine "ResultEvent" (which is capture a screenshot and email it out), then it goes to Exit>0
2.before it exits, it tries to complete the step that it was working on when time ran out
3.redoes "ResultEvent", and then closes.

Any ideas is appreciated!

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Possible to kill macro after set duration?

Post by JRL » Mon Feb 06, 2017 8:51 pm

I see what you mean. I created a sample and it ran the sub twice before closing also. Solved it by adding a variable to flag whether the routine had already run. See example below

Code: Select all

Let>timerFlag=0


Let>TimePeriod=120
Timer>StartTime
OnEvent>Custom,EventTimer,TimedOut,ResultEvent
SRT>EventTimer
  Timer>EndTime
  Let>TotalTime={(%EndTime%-%StartTime%)/1000}
  If>%TotalTime%>%TimePeriod%
    Let>TimedOut=True
  EndIf
END>EventTimer
SRT>ResultEvent
  If>TimerFlag=0
    Let>TimerFlag=1
    //Start process that will wait for this script to exit
    //then restart this script
   
    //Do any closing processes needed before exit
   
    Exit>0
  EndIf
END>ResultEvent
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Etc

headgeek
Newbie
Posts: 8
Joined: Wed Feb 01, 2017 9:11 pm

Re: Possible to kill macro after set duration?

Post by headgeek » Tue Feb 07, 2017 3:09 pm

Perfect, Thank you!

I can't begin to tell you how much time I spent looking at the log file and reviewing the code. I was positive that I messed up the original code by adding in my additional instructions (as by itself it worked fine, as it was basically two exits)

That fix is so simple, yet something I wouldn't have come up with. Adding this to my knowledge bank.

Again, Thank you!

headgeek
Newbie
Posts: 8
Joined: Wed Feb 01, 2017 9:11 pm

Follow Up Question - How to Pause Timer

Post by headgeek » Thu Feb 16, 2017 6:44 pm

Just a quick follow up question: I have one step in that could take way longer.

What's the best way to pause the timer?
Only Idea that I have is to give the timer a crazy high second count.

Let>TimePeriod=12000000000

after the step is completed, I will put it back

Let>TimePeriod=120

Any other ideas?

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Possible to kill macro after set duration? - SOLVED

Post by JRL » Thu Feb 16, 2017 7:18 pm

Sounds like it will work and only adds two lines of code. I would call that THE solution.

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: Possible to kill macro after set duration? - HELP

Post by theonex » Sun Sep 17, 2017 5:30 pm

Hello

i know this post was solved and closed but i need a little help understanding it i am using notepad as an example.
i should have 2 possible end results. option 1 text in notepad is found and script is complete with message>process complete. if text is not found i would like the notpad to be closed and the process restarted.

what is wrong with how i am using the below script.

Code: Select all

Let>timerFlag=0

Let>TimePeriod=20
Timer>StartTime
OnEvent>Custom,EventTimer,TimedOut,ResultEvent
SRT>EventTimer
  Timer>EndTime
  Let>TotalTime={(%EndTime%-%StartTime%)/1000}
  If>%TotalTime%>%TimePeriod%
    Let>TimedOut=True
  EndIf
END>EventTimer
SRT>ResultEvent
  //Start process that will wait for this script to exit
  //then restart this script
    
  //Do any closing processes needed before exit
   Message>Process not complete
   wait>5
  //Do any closing processes needed before exit
  CloseWindow>notepad*
  wait>1
  Exit>0
END>ResultEvent



//Stuff in your current script
RunProgram>C:\Windows\notepad.exe
wait>2
SendText>macro scheduler is the best
wait>1
Timer>StartTime


//Stuff in your current script
GetTextInit
GetWindowTextEx>Untitled - Notepad,result
WaitScreenText>macro scheduler is the best
wait>2
Timer>StartTime



//Stuff in your current script
Message>process completed!! :)
wait>5
Timer>StartTime

User avatar
JRL
Automation Wizard
Posts: 3501
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Possible to kill macro after set duration? - SOLVED

Post by JRL » Mon Sep 18, 2017 1:56 pm

i know this post was solved and closed but i need a little help understanding it...
First your notepad script has no reason to fail so no likelihood of matching your "text is not found" scenario. But mainly the script I presented is a vague outline and shouldn't be taken so literally.

The premise to use a script like this is that you have a program that sometimes fails or sometimes fails to produce a given result in a predefined time frame. If you have this situation then you would introduce elements of my script into specific locations within your script. headgeek needed to restart his script in the condition that it did not accomplish a given task within a set amount of time. When that situation occurred the custom OnEvent> would trigger and a second script would be started. That second script would wait for the primary script to close and then restart the primary script.

I have an idea for a notepad example. I'll see if I can get that written and posted later today.

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: Possible to kill macro after set duration? - SOLVED

Post by theonex » Thu Sep 21, 2017 10:02 am

just wanted to update the post i did mange to solve the issue. I had the 2 codes mixed so there was these 2 lines missing from my code

Code: Select all

  If>TimerFlag=0
    Let>TimerFlag=1

If you have the Endif after the Exit>0 line the script is not exited but it stops so putting the Endif before the Exit>0 line works great.

Code: Select all

Let>timerFlag=0


Let>TimePeriod=120
Timer>StartTime
OnEvent>Custom,EventTimer,TimedOut,ResultEvent
SRT>EventTimer
  Timer>EndTime
  Let>TotalTime={(%EndTime%-%StartTime%)/1000}
  If>%TotalTime%>%TimePeriod%
    Let>TimedOut=True
  EndIf
END>EventTimer
SRT>ResultEvent
  If>TimerFlag=0
    Let>TimerFlag=1
    //Start process that will wait for this script to exit
    //then restart this script
   
    //Do any closing processes needed before exit

  EndIf   
    Exit>0
END>ResultEvent
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Stuff in your current script
Timer>StartTime
//Etc

theonex
Junior Coder
Posts: 44
Joined: Thu May 10, 2012 12:32 pm

Re: Possible to kill macro after set duration? - SOLVED

Post by theonex » Mon Mar 12, 2018 4:18 am

Hello,

I have been using JRL`s Custome onevent and it works great it does the job of restarting the script if for any reason the application you are controlling hangs and a given amount of time passes. However i is it possible to use to custom time based onevent handlers?

e.g
/////////////////////Restart Script If No Progress///////////////////////
Let>timerFlag=0
Let>TimePeriod=30
Timer>StartTime
OnEvent>Custom,EventTimer,TimedOut,ResultEvent
SRT>EventTimer
Timer>EndTime
Let>TotalTime={(%EndTime%-%StartTime%)/1000}
If>%TotalTime%>%TimePeriod%
Let>TimedOut=True
EndIf
END>EventTimer
SRT>ResultEvent
If>TimerFlag=0
Let>TimerFlag=1
//Start process that will wait for this script to exit
//then restart this script

//Do any closing processes needed before exit

Message>Script Did Not Complete Successfully
wait>5

Endif
// Exit>0
END>ResultEvent
/////////////////////////////////////////////////////////////////////////
Let>timerFlag=0
Let>TimePeriod=15
Timer>MyTriggerSub
OnEvent>Custom,EventtTimerr,TimedOut,ResulttEventt
SRT>EventtTimerr
Timer>EndTime
Let>TotalTime={(%EndTime%-%MyTriggerSub%)/1000}
If>%TotalTime%>%TimePeriod%
Let>TimedOut=True
EndIf
END>EventtTimerr
SRT>ResulttEventt
If>TimerFlag=0
Let>TimerFlag=1
//Start process that will wait for this script to exit
//then restart this script

//Do any closing processes needed before exit

SkipLabel>option B


Endif
// Exit>0
END>ResulttEventt
///////////////////////////////////////////////////

While>a=0
Let>b=b+1
Let>tag=tags_%b%
Pos>id="lst-ib",tag,1,a
EndWhile
Timer>MyTriggerSub

/////////////////////////////////////////////////
Label>option B
Timer>StartTime

IEWaitNew>IE[1]
Timer>StartTime

With the above example Timer>MyTriggerSub is triggered and since Timer>MyTriggerSub sends you to Label>Option B and since there is no new IE opened Timer>StartTime should kick in but it does not any ideas to possible solutions.

Thank you

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