Hello Guest it is March 29, 2024, 12:36:21 AM

Author Topic: Code Execution order is reversed  (Read 4082 times)

0 Members and 1 Guest are viewing this topic.

Code Execution order is reversed
« on: February 25, 2008, 06:29:25 AM »
I've found what, to me, seems a strange behaviour in M3. In a macro I have, in effect, the following sequence:

for i%=1 to 2
  Call SetVar(123, i%)
  Code "M98 (mysub" & i% & ".tap) L1"
next i%

The problem is that neither of the subroutines get executed until after the entire macro has run. And then they are run in reverse order: mysub2.tap first, then mysub1.tap . And when they read #123 they see value2. It is as though I had asked for:

  Call SetVar(123, 1)
  Call SetVar(123, 2)
  Code "M98 (mysub2.tap) L1"
  Code "M98 (mysub1.tap) L1"

I have found a solution that works for my present situation. Change the above to:
  OpenTeachFile(myfile.tap)
    for i%=1 to 2
      Code "#123=" & i%
      Code  "M98  (mysub" & i% & ".tap) L1"
    next i%
  CloseTeachFile()
  LoadTeachFile()

I haven't experimented to see how the execution of other Code works, but it seems you can't count on it being executed in order or at the time the Code function is executed. The Mach2 Customization Guide says you're not advised to call another script from within a script. But is M98 a script? Does this mean you shouildn't use any M commands in a Code function in a macro? Or does it affect only M98(file) ?

Can anyone enlighten me please?

Thanks
Chris

« Last Edit: February 25, 2008, 06:50:01 AM by ChrisLT »

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: Code Execution order is reversed
« Reply #1 on: February 25, 2008, 10:53:59 AM »
Hi Chris

The VB under Mach is a little... well let's say idiosyncratic  ::)

I suspect you are falling foul of the caveat in the UG because you are indeed effectively calling a script from within a script. The finer workings of Mach are of course only known to the inner sanctum of Art (retired) & Brian et al (not sure if there actually is an "et al" or whether its just poor Brian on his tod these days  ;D)

So... You run your script. This is running in a separate thread to Mach. Your script sets #123 to 1 and then makes a "code" call to Mach - effectively requesting Mach to run mysub1.tap - Mach will do this in it's own good time. Meanwhile your script whips round the loop, sets #123 to 2 and requests Mach to run mysub2.tap which again, it will do when it's good and ready. Meanwhile your code whips round - sees i%=2 and jumps to the line after the next which is the end of your script - it closes down the thread.

Back to Mach - Mach looks on it's "VB request pending buffer" (my made up name for this bit of Mach) which I'm guessing is a stack (LIFO (last in first out)) rather than a queue (FIFO (first in first out)) and hence runs mysub2.tap. Of course by this time 123 is 2. That done it pops the next request from the stack - mysub1.tap and of course #123 is still 2.

FWIW you could try putting a "while isMoving() wend" construct after the call to "code". This will cause your loop to wait on a sync semaphore from the Mach thread before continuing. A reason why this may not work is that while your code is waiting, Mach will run your mysubX.tap. IF the VB interpreter is non-reentrant then it's still going to fail (which may be why there is the caveat in the UG).

This of course may be quite wrong - but it's my best guess.

Cheers

Ian
Re: Code Execution order is reversed
« Reply #2 on: February 28, 2008, 09:22:42 PM »
Hi Ian,
Thanks for this explanation. It confirms what I imagined must be happening. Actually, getting my macros to populate a Teach File is no great hardship and seems to cure the problem. I'm not convinced isMoving() would do it, but it might be worth a try.
Cheers
Chris