BEFORE SEQUENCE FINISH DO behavior is - inconvenient
Posted: Sun Jul 21, 2024 7:12 pm
Ran into a circular logic loop that I couldn't escape using "BEFORE SEQUENCE FINISH DO" blocks. I assume this is for cleanup when errors occur, which is what I have been using it for (stopping guiding, homing the mount and such). Apologies at the start for the long-winded post - this is complicated.
Firstly, this doesn't work:
SEQUENCE
DEF SUB ErrorHandler
PROMPT "Error handler entered hit ok to continue" ENABLECONTROLS True
END SUB
DEF SUB ErrorHandlerDisable
BEFORE SEQUENCE FINISH DO
GOSUB ErrorHandler
END DO
END SUB
DEF SUB ErrorHandlerEnable
BEFORE SEQUENCE FINISH DO
END DO
END SUB
GOSUB ErrorHandlerEnable
....more code
GOSUB ErrorHandlerDisable
END SEQUENCE
It seems that error handler blocks can not be created within a subroutine. I think I understand the logic why this is done this way as its about the sequence not the subroutine (since the subroutine might be in some imported sequence - messy). This is the setup for not being able to escape the following problem:
Test sequence:
SEQUENCE
DEF SUB ErrorCleanup
PROMPT "Error cleanup entered - hit ok to continue" ENABLECONTROLS True
END SUB
BEFORE SEQUENCE FINISH DO
GOSUB ErrorCleanup
END DO
RUN SEQUENCE FROM "\\Mac\Home\Desktop\SharpCap Captures\__Sharpcap Sequences\_Test Sequences\SEQ Error Handler 2.scs"
PROMPT "We are now back in the main sequence" ENABLECONTROLS True
END SEQUENCE
SEQ Error Handler 2.scs code is just:
SEQUENCE
PROMPT "We executing the second sequence file. Hit ok to return to main sequence" ENABLECONTROLS True
END SEQUENCE
When I run this test sequence, I get the following series of prompts:
1) Prompt dialog "We executing the second sequence file. Hit ok to return to main sequence"
2) Prompt dialog "Error handler entered - hit ok to continue"
3) Prompt dialog "We are now back in the main sequence"
4) Prompt dialog "Error handler entered - hit ok to continue"
So, my the end of sequence gets called after ANY sequence that is executed, rather than just when the top level sequence ends. So, the only way to safely use BEFORE SEQUENCE FINISH DO blocks is to encapsulate any code you use in other sequences in SUBs and import them to prevent inappropriately triggering the current BEFORE SEQUENCE FINISH DO block.
It seems to me that since BEFORE SEQUENCE FINISH DO blocks were implemented in such a way that they can not be defined inside a function, my guess is that they were intended to only apply to the sequence the BEFORE SEQUENCE FINISH DO was defined in, so each sequence executed can have its own finish up block independent of all others. As it is now, its just a mess, with blocks getting executed at inappropriate times. The only current work around is to undefine and redefine BEFORE SEQUENCE FINISH DO blocks before/after running another sequence, which also gets messy very fast.
Its seems it should be farly easy to run afoul of the end of sequence block problem, but I did it in style:
I had some Python code that created a short SEQUENCE in a file (call it "File A.scs") with a standardized name and location that then gets executed from the sequence that ran the Python code that generated "File A.scs". This ran smack into the issue described above, of course. I switched to defining functions in "File A.scs". However, this has its own problems as then I have to execute the sequence-generating python code at just the right time before the importing "File A.scs" so I can all the subtroutines defined therein. And just to make it all more complicated, sequence imports can't be done from within a subroutine (which is totaly understandable), but rather are only honored if they are at the lowest scope within a sequence. Luckily, imports aren't performed all at once at the start of a sequence, but rather as the import commands are encountered during sequence execution, or I would really be up a creek.
So, to summarize:
1> The whole mess above would be avoided if BEFORE SEQUENCE FINISH DO blocks only apply within the sequence within which they are defined.
2> When a sequence file is started, its BEFORE SEQUENCE FINISH DO blocks is set to empty, and restored to empty when it completes (after executing the BEFORE SEQUENCE FINISH DO block that was active when the end of sequence was reached).
3> When a sequence file runs a second sequence, its current BEFORE SEQUENCE FINISH DO block is saved prior to the second sequence executing and restored after the second sequence completes executing.
As it is now, some surprising behaviors can happen - I may have actually run into them and branched around with by code re-write and not understood what was actually going amiss.
Firstly, this doesn't work:
SEQUENCE
DEF SUB ErrorHandler
PROMPT "Error handler entered hit ok to continue" ENABLECONTROLS True
END SUB
DEF SUB ErrorHandlerDisable
BEFORE SEQUENCE FINISH DO
GOSUB ErrorHandler
END DO
END SUB
DEF SUB ErrorHandlerEnable
BEFORE SEQUENCE FINISH DO
END DO
END SUB
GOSUB ErrorHandlerEnable
....more code
GOSUB ErrorHandlerDisable
END SEQUENCE
It seems that error handler blocks can not be created within a subroutine. I think I understand the logic why this is done this way as its about the sequence not the subroutine (since the subroutine might be in some imported sequence - messy). This is the setup for not being able to escape the following problem:
Test sequence:
SEQUENCE
DEF SUB ErrorCleanup
PROMPT "Error cleanup entered - hit ok to continue" ENABLECONTROLS True
END SUB
BEFORE SEQUENCE FINISH DO
GOSUB ErrorCleanup
END DO
RUN SEQUENCE FROM "\\Mac\Home\Desktop\SharpCap Captures\__Sharpcap Sequences\_Test Sequences\SEQ Error Handler 2.scs"
PROMPT "We are now back in the main sequence" ENABLECONTROLS True
END SEQUENCE
SEQ Error Handler 2.scs code is just:
SEQUENCE
PROMPT "We executing the second sequence file. Hit ok to return to main sequence" ENABLECONTROLS True
END SEQUENCE
When I run this test sequence, I get the following series of prompts:
1) Prompt dialog "We executing the second sequence file. Hit ok to return to main sequence"
2) Prompt dialog "Error handler entered - hit ok to continue"
3) Prompt dialog "We are now back in the main sequence"
4) Prompt dialog "Error handler entered - hit ok to continue"
So, my the end of sequence gets called after ANY sequence that is executed, rather than just when the top level sequence ends. So, the only way to safely use BEFORE SEQUENCE FINISH DO blocks is to encapsulate any code you use in other sequences in SUBs and import them to prevent inappropriately triggering the current BEFORE SEQUENCE FINISH DO block.
It seems to me that since BEFORE SEQUENCE FINISH DO blocks were implemented in such a way that they can not be defined inside a function, my guess is that they were intended to only apply to the sequence the BEFORE SEQUENCE FINISH DO was defined in, so each sequence executed can have its own finish up block independent of all others. As it is now, its just a mess, with blocks getting executed at inappropriate times. The only current work around is to undefine and redefine BEFORE SEQUENCE FINISH DO blocks before/after running another sequence, which also gets messy very fast.
Its seems it should be farly easy to run afoul of the end of sequence block problem, but I did it in style:
I had some Python code that created a short SEQUENCE in a file (call it "File A.scs") with a standardized name and location that then gets executed from the sequence that ran the Python code that generated "File A.scs". This ran smack into the issue described above, of course. I switched to defining functions in "File A.scs". However, this has its own problems as then I have to execute the sequence-generating python code at just the right time before the importing "File A.scs" so I can all the subtroutines defined therein. And just to make it all more complicated, sequence imports can't be done from within a subroutine (which is totaly understandable), but rather are only honored if they are at the lowest scope within a sequence. Luckily, imports aren't performed all at once at the start of a sequence, but rather as the import commands are encountered during sequence execution, or I would really be up a creek.
So, to summarize:
1> The whole mess above would be avoided if BEFORE SEQUENCE FINISH DO blocks only apply within the sequence within which they are defined.
2> When a sequence file is started, its BEFORE SEQUENCE FINISH DO blocks is set to empty, and restored to empty when it completes (after executing the BEFORE SEQUENCE FINISH DO block that was active when the end of sequence was reached).
3> When a sequence file runs a second sequence, its current BEFORE SEQUENCE FINISH DO block is saved prior to the second sequence executing and restored after the second sequence completes executing.
As it is now, some surprising behaviors can happen - I may have actually run into them and branched around with by code re-write and not understood what was actually going amiss.