How to wait for completion of a script

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
DanielWenger
Posts: 1
Joined: Mon Nov 19, 2018 12:36 am

How to wait for completion of a script

Post by DanielWenger » Mon Nov 19, 2018 12:49 am

I have a handler that calls a series of button scripts. I want to wait after each "send mouseUp" until the script in that button has completed before calling the next button script.

-- a handler in my card script

on myHandler
send mouseUp to cd button Num1
wait until (??) -- what do I put here
send mouseUp to cd button Num2
wait until (??) -- what do I put here
etc
end myHandler

++++++++++++++++++++++
handler on cd button Num1

on mouseUp
doSomething1 -- a call to another handler
doSomething2 -- a call to another handler
etc.
pass (??) -- is this correct
end mouseUp

Thanks for any help here. Probably a very simple solution but it evades me.

Daniel

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to wait for completion of a script

Post by Klaus » Mon Nov 19, 2018 10:53 am

Hi Daniel,

hello and welcome to the forum! 8)

Well, this depends on what dosomething1 etc. really does!
Can't you just put everything into your myHandler?

Code: Select all

on myHandler
  ## send "mouseUp" to cd button Num1
  ## wait until (??) -- what do I put here
  doSomething1 
  doSomething2
  ## send mouseUp to cd button Num2
  ##wait until (??) -- what do I put here
  doSomething3 
  doSomething4
  ## etc
end myHandler
However ususally it should work, since the dictionary (hint, hint) about "send" tells me:
...
Important: Specifying a time can affect the order in which statements are executed. If you don't specify a time, the message is sent immediately, and any handler it triggers is completed before the rest of the current handler is executed.
...

Well meant advice:
Always put quotes around strings, object names and the message to send and since LC <> HC, no need to name your controls with CD:

Code: Select all

...
## send mouseup to cd btn Num1
send "mouseup" to btn "Num1"
...
Best

Klaus

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to wait for completion of a script

Post by bogs » Mon Nov 19, 2018 1:30 pm

Klaus has it right, when you start a script from another object, function, handler, whatever, Lc runs that script, then comes back to where you requested it, it isn't doing things simultaneously (multi-threaded).

You can see this in action yourself in the debugger, set a break point at the beginning of your handler, then step through as the code executes. You'll see it go from the handler to the button, back to the handler, then to the next and so on and so forth, no waiting required.
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to wait for completion of a script

Post by dunbarx » Mon Nov 19, 2018 3:47 pm

All good stuff.

I just want also to suggest that you consider the advantages of having all your functionality in a single handler if possible.

The times where individual handlers might best be placed in buttons is when the user is expected to click on those buttons. That might be the case with you. But the fact that you invoke them from another source makes me want to at least expose you to another way.

It is far easier to maintain a single script. You can use the "target" to run whatever particular functionality you require. Say you had three buttons, "B1". "B2" and "B3". Perhaps in a card script:

Code: Select all

on mouseup
   switch the short name of the target
      case "b1"
         doB1Stuff
         break
      case "b2"
         doB2Stuff
         break
      case "b3"
         doB3Stuff
         break
   end switch
end mouseup
Click on any button, and its code portion will fire. This is all just so you see the possibilities.

Craig Newman

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”