Stop a macro

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Dj Telo
Junior Coder
Posts: 24
Joined: Sun Mar 21, 2010 7:27 pm

Stop a macro

Post by Dj Telo » Sun Mar 28, 2010 5:03 pm

Hi everyone!

Is there any way to stop a running macro if its normal execution time is exceeded? For instance, I've programmed a macro which scrolls down a web page until an image is found. If that image never shows, the macro will never exit the loop. A counter in the loop is not a suitable solution for what I need. Once I found a macro running for more than 2 hours and I'd want to avoid this.

Any help, please?

Thank you!

JeffDaruecan
Newbie
Posts: 18
Joined: Thu Mar 25, 2010 11:00 am
Location: Vancouver

Post by JeffDaruecan » Sun Mar 28, 2010 9:48 pm

Hope this helps. Just remove the MDL> and place your script there.
It will then allow the script to loop only as many times as determined by the until> value.

although it is a counter, It may still work just time how long your single loop is. If running your script loop for finding the image takes only 5 seconds, then its a simple 5:1 seconds to timeout ratio and if you want it to time out after 2 minutes you would change Until>TO,24

Code: Select all

Let>TO=0
  Repeat>TO
  Add>TO,1
  \\Script here
  MDL>Just change Until>TO, to however many loops you want %TO%
Until>TO,5

Dj Telo
Junior Coder
Posts: 24
Joined: Sun Mar 21, 2010 7:27 pm

Post by Dj Telo » Sun Mar 28, 2010 10:29 pm

Hi Jeff!

I've followed your instructions blindly and it seems to be working.

Anyway I still don't understand why, I don't understand what the structure you suggested is doing in my script. Could you please explain it to me? I want to understand it well just in case I need to use something similar in the future.

Thank you for your time. ;)

JeffDaruecan
Newbie
Posts: 18
Joined: Thu Mar 25, 2010 11:00 am
Location: Vancouver

Post by JeffDaruecan » Mon Mar 29, 2010 1:41 am

I don't fully understand it all myself as I'm still rather new to using MS.
But this is how this one is working.
Let>TO=0 assigns the numeric value of the word TO to equal 0 *can be changed to anything I selected TO as an abbreviation of TimeOut*

Repeat>TO shows where to continue the script if the until value is not reached

the Add>TO,1 means simply every time the script passes this code it will add +1 to the value of TO. So on every loop it goes 0,1,2,3,4, Etc.

Until>TO,5 takes you back to Repeat>TO unless the TO value equals 5, or whatever you have it set to.

So as long as you have the script after the Add but before the until it will repeat that section as many times Until the value becomes true.
If you have it searching for an img you could insert something like

IF>img=1
goto>image found

then after the looping have make a Label>image found

and put the rest of the script that happens after that image is found. it will quickly and easily get you out of the loop when the image is found.

Just a side thought if not already done, under Until add and Exit code or an error dialog that way when it times out it will bring up an error or instantly close the macro.

heres a quick example of what I mean with the if>img, the label, error message then exit.

Have fun tinkering around with the script and hope you get it all working properly and able to understand why and how its working.

Code: Select all

Let>TO=0
  Repeat>TO
  Add>TO,1
\\your script
IF>img=1
  Goto>Img Found
EndIf
Until>TO,5
MDL>Image Not Found
Exit

Label>Img Found
\\Rest of your script when image is found

Dj Telo
Junior Coder
Posts: 24
Joined: Sun Mar 21, 2010 7:27 pm

Post by Dj Telo » Mon Mar 29, 2010 10:35 am

Thaks again for your answer.

Now I understand what you mean and I realize that what you suggested is a basic loop with a counter, which I have already been doing in my scripts.

Probably I didn't explain correctly what I needed to do. Let's start over.

In some of my scripts there must be some errors which I am still not able to find and sometimes the script doesn't pass the code entirely. So, sometimes I find my computer stuck while running a script and from the time I secheduled the macro I know it's been that way for hours and hours. My computer is then very hot and the "I am thinking" led flashes crazily.

Said so, what I need to do is, if my script is supposed to run entirely in, let's say, 10 seconds, if 2 minutes have passed and it still hasn't finished (which probably means it definitely won't) the computer should stop the macro.

It seems to me that it should be something in the code that counts the time passed since the macro started and, if it passes a limit value, it would stop the process no matter how far it's gone.

I'm sorry for my English, I just try to do my best.

Thanks Jeff. ;)

JeffDaruecan
Newbie
Posts: 18
Joined: Thu Mar 25, 2010 11:00 am
Location: Vancouver

Post by JeffDaruecan » Mon Mar 29, 2010 12:08 pm

If your script is constantly getting stuck in an infinite loop then there is something wrong with the script. Have you tried using the debugger and logs to find what is causing the problem?


you can try creating a WriteLn that will create and run a secondary script with a wait time for however long you want it to run for, after the wait time have it end the process which is running the main script. Since it would be on a secondary script it shouldn't be affected by any infinite loop problems.

I don't know enough about using WriteLn yet to be able to help, I can only tell you that it could be the next step to try... after debugging your script to ensure there isn't a small mistake somewhere of course.

Dj Telo
Junior Coder
Posts: 24
Joined: Sun Mar 21, 2010 7:27 pm

Post by Dj Telo » Wed Mar 31, 2010 3:42 pm

The problem was in the WaitScreenImage command. Sometimes, I don't know why, the image to find was there but it wouldn't find it. I've changed every WaitScreenImage in my script for basic Waits and it is working just fine now.

Thank you for your help! ;)

JeffDaruecan
Newbie
Posts: 18
Joined: Thu Mar 25, 2010 11:00 am
Location: Vancouver

Post by JeffDaruecan » Wed Mar 31, 2010 9:18 pm

You should have posted your script. As I'm sure everyone would have told you to add the following

Let>WSI_TIMEOUT=10 *number of seconds*
WaitScreenImage>
If>WSI_TIMEOUT=TRUE
LibFunc>user32,MessageBoxA,r,0,Image Not Found,Error,16
Exit
endIf

JamieMTL
Newbie
Posts: 1
Joined: Mon Nov 07, 2022 3:18 pm

Re: Stop a macro

Post by JamieMTL » Mon Nov 07, 2022 3:22 pm

@ JeffDaruecan

I believe you mean "WSI_TIMEDOUT" and not "WSI_TIMEOUT"

Jamie

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