Jump to content

Script keeps running after adlib?


Recommended Posts

I'm working on a script that needs to listen for errors to pop up, but I find that after it handles the first error, things go crazy because its as if my main script is continuing to run. Is there a way to tell my main script to stop running if it hits an adlib? I tried Exit, but that stops the whole thing, including the adlib. I hope this makes sense!

Thank you!

HotKeySet("{ESC}", "Terminate")

Call("main")
Func main()
WinActivate("[CLASS:XLMAIN]") ;This step activates Excel, strikethrough, arrow down, copy
Sleep(400)
Send("^5")
Sleep(400)
Send("{DOWN}")
Sleep(400)
Send("^c")
Sleep(400)
WinActivate("[CLASS:MRWF]") ;This step acivates my target program, pastes what was copied, then enters text.
   AdlibRegister("zerobal") ;But at this point, it might hit an error, so I need it to stop the main function and go down to this adlib, which it does, but main seems to keep running as well
Sleep(800)
Send("!v")
Sleep(500)
   AdlibUnRegister("zerobal")
   AdlibRegister("agency") ;if the previous chokepoint was passed, there's the possibility of another error here, so I need adlib to handle it and main to stop and restart after adlib handled it.
Send("text goes here")
Sleep(500)
Send("{ENTER}")
Sleep(500)
Send("Y")
Send("{ENTER}")
Sleep(500)
   AdlibUnRegister("agency")
EndFunc

Func Terminate()
    Exit
EndFunc

Func agency()
   If WinExists("Yes/No Confirmation") Then
      Sleep(500)
      Send("Y")
      Sleep(500)
      Call("main") ;This is the end of a successful run, I just need it to start over, but it gets hinky because the main kept running so its like main is running twice at once.
   EndIf
   If WinExists("Error") Then
      Sleep(500)
      Send("{ENTER}")
      Sleep(500)
      Send("{F10}")
      Sleep(500)
      Send("{F11}")
      Sleep(500)
      Send("Y")
      Call("main") ;if it hit this error, I need it to handle it with these steps, but on the next run of main, it gets hinky.
   EndIf
EndFunc

Func zerobal()
   If WinExists("Error") Then
      Sleep(500)
      Send("{ENTER}")
      Sleep(500)
      Send("{F10}")
      Sleep(500)
Call("main") ;Same deal down here if it hit this error
   EndIf
EndFunc
Link to comment
Share on other sites

You seem to have an oxymoron, in that you want the main script to close but not close.

So you need to rethink your logic.

[1] Your main script, is the one that starts adlib running, and then continues to run itself.

[2] Your secondary code in the adlib function, needs to check for errors at the specified time, then run something when an error is found.

[3] Meanwhile, your main script should still be running.

[4] Your main script may still be calling the adlib function, and thus just keep repeating.

[5] Is that error resolved?

[6} If necessary, use AdlibUnRegister and then restart adlib after a prescribed duration.

[7] However, Step 6 should not be necessary, and may still react to the same error if it hasn't been reconciled.

In any case, if your Adlib function is written properly, then once complete it should have finished running.

Maybe you need to disable checking (AdlibUnRegister) while your Adlib function (error found code) is running, then re-enable (AdlibRegister) again after it finishes.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

I'm working on a script that needs to listen for errors to pop up, but I find that after it handles the first error, things go crazy because its as if my main script is continuing to run. Is there a way to tell my main script to stop running if it hits an adlib? I tried Exit, but that stops the whole thing, including the adlib. I hope this makes sense!

Thank you!

When an adlib registered function is called, the main code ceases running (paused) until the adlib function returns.

If you want to repeat your main code, then think of a loop. You then can use ContinueLoop and Exitloop to control the flow. Recalling functions within functions is going to most likely cause issues.

While 1
    ; code to repeat here
WEnd

A registered adlib function is best to be independent from the main code. Calling the main function from an adlib function is trying to interrupt with the flow of the main code and is going to cause issues. It is usually good for the adlib function to assist with events rather then trying to dominate the flow of the main code based on the events.

If Send is going to be the main method of interaction, then consider using SendKeepActive as the Sent keys will become less error prone to fail due to unexpected reasons. I have done in the past some tests with SendKeepActive and it seems to make the windows like magnetic to the sent keys with keeping activation. SendKeepActive works surprising well and deserves more attention then what it currently gets.

I have probably broken more then I fixed as the code being automation based. It is more of a hack rather then a clean rewrite. Automation is quite testing yet alone my guessing with code changes to make a ideal script of your code.

So, this is more of a guide rather then a final solution. Guaranteed to work? No. I consider that it could be improved.

HotKeySet("{ESC}", "Terminate")
Opt('SendKeyDelay', 500) ; This slows down all Sent keys sl all those Sleeps are not needed.

; Want to main code going, use a loop.
While 1
    If SendKeepActive("[CLASS:XLMAIN]") Then ;This step activates Excel, strikethrough, arrow down, copy
        Send("^5")
        Send("{DOWN}")
        Send("^c")
        If SendKeepActive("[CLASS:MRWF]") Then ;This step acivates my target program, pastes what was copied, then enters text.
            zerobal("[CLASS:MRWF]") ;But at this point, it might hit an error, so I need it to stop the main function and go down to this adlib, which it does, but main seems to keep running as well
            If @error Then ContinueLoop
            Sleep(300) ; + delay = 800
            Send("!v")
            agency("[CLASS:MRWF]") ;if the previous chokepoint was passed, there's the possibility of another error here, so I need adlib to handle it and main to stop and restart after adlib handled it.
            If @error Then ContinueLoop
            Send("text goes here")
            Send("{ENTER}")
            agency("[CLASS:MRWF]")
            If @error Then ContinueLoop
            Send("Y")
            Send("{ENTER}")
            agency("[CLASS:MRWF]")
            If @error Then ContinueLoop
        EndIf
    EndIf
WEnd

Func Terminate()
    Exit
EndFunc

Func agency($resume_title = '')
    Local $flag_error ; if an event occurs, this will be set to true so that error is set at the end of the function.
    If WinActivate("Yes/No Confirmation") Then
        Send("Y")
        $flag_error = True ;This is the end of a successful run, I just need it to start over, but it gets hinky because the main kept running so its like main is running twice at once.
    EndIf
    If WinExists("Error") And SendKeepActive("Error") Then
        Send("{ENTER}")
        Send("{F10}")
        Send("{F11}")
        Send("Y")
        $flag_error = True ;if it hit this error, I need it to handle it with these steps, but on the next run of main, it gets hinky.
    EndIf
    If Not ($resume_title == '') Then SendKeepActive($resume_title)
    If $flag_error Then SetError(1)
EndFunc

Func zerobal($resume_title = '')
    Local $flag_error
    If WinExists("Error") And SendKeepActive("Error") Then
        Send("{ENTER}")
        Send("{F10}")
        $flag_error = True ;Same deal down here if it hit this error
    EndIf
    If Not ($resume_title == '') Then SendKeepActive($resume_title)
    If $flag_error Then SetError(1)
EndFunc

I used an Opt() to cause a send keys delay so as to get rid of most of those Sleeps.

Edit: Update zerobal function with a SendKeepActive call.

Edited by MHz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...