How does Livecode handle concurrent events?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
KimD
Posts: 211
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

How does Livecode handle concurrent events?

Post by KimD » Fri Oct 23, 2015 3:06 am

I learnt to programme about the time that disco was giving way to punk & reggae. Then I took a 30 year break. Now I'm trying to get back into it. Could someone explain to me how Livecode processes concurrent events?

For example:

On LocationChanged
CustomHandlerABC
End LocationChanged

On MouseUp // for a button
CustomHandlerXYZ
End Mouseup

Lets assume that a mobile device, with an active GPS, detects a change in location. The device now runs CustomHandlerABC. What happens if I push the button associated with CustomHandlerXYZ while CustomHandlerABC is still executing?

1) Does livecode finish executing CustomHandlerABC and ignore the button push; or
2) Does livecode finish executing CustomHandlerABC then execute CustomHandlerXYZ; or
3) Does livecode execute CustomHandlerABC and CustomHandlerXYZ in parallel /* which would send my procedural programming trained brain into a complete spin */; or
4) Something else?

Thanks in advance

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8421
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How does Livecode handle concurrent events?

Post by FourthWorld » Fri Oct 23, 2015 3:32 am

#2. There are a few exceptions in which messages can be processed while other handlers are paused or awaiting data from the OS (e.g. "wait <time> with message". or using socket I/O with callbacks), but for the most part user-driven messages are queued while a handler is running, and processed when the handler is done.

And FWIW, good instincts on #3. Multithreading and multiprocessing can be useful for some types of tasks, but require some deep thinking about state locks to be of use to a main thread; given the scope of literature on the subject yours isn't the only head that folds in on itself when trying to handle coordinated concurrency. :) In general, LiveCode, like Python, is single-threaded by design.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

KimD
Posts: 211
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: How does Livecode handle concurrent events?

Post by KimD » Fri Oct 23, 2015 4:07 am

Thanks Richard. I'm struggling enough with object orientated, which all seems a bit wild & hippy, without needing to think about multi-threaded hippy ;-)

And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8421
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How does Livecode handle concurrent events?

Post by FourthWorld » Fri Oct 23, 2015 6:47 am

KimD wrote:Thanks Richard. I'm struggling enough with object orientated, which all seems a bit wild & hippy, without needing to think about multi-threaded hippy ;-)
That's one of the things I like about LiveCode: it has objects, but isn't pure OOP. In a true OOP you need to instantiate a math class just to add two numbers - ugh. I like to think of LiveCode as being more "object-based", which may be harder for OOP purists to get into, but the deep integration with a simple and sensible GUI object model makes it very productively enjoyable for the rest of us.
And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?
The former. There are ways to handle messages during long processes if needed, but for most things you'll never need to worry about that.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: How does Livecode handle concurrent events?

Post by dunbarx » Fri Oct 23, 2015 2:09 pm

And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?
As Richard has explained, "object based" being an apt description, everything runs at a "high" level. Try this. Make two buttons and a field. In the first button script:

Code: Select all

on mouseUp
   runCounter
end mouseUp

on runCounter
   if the optionKey is down then exit runCounter --the only way out of this mess, as written
   add 1 to fld 1
   wait 60
   --wait 60 with messages
   runCounter
end runCounter
In the second button script:

Code: Select all

on mouseUp
   answer "Whew! I am outta here"
end mouseUp
Click the first button. You see the field incrementing second by second. If you click the second button, nothing happens, because the "wait" command is blocking. But what happens when you press the optionKey?

Now comment out the "wait 60" line, and uncomment the "wait 60 with messages" line. Click button 1, and watch the counter. Now click button 2. Might this be considered an interrupt, implemented at a "high" level? Other cute methods abound...

Craig Newman

KimD
Posts: 211
Joined: Wed Jul 08, 2015 5:51 am
Location: Wellington, New Zealand

Re: How does Livecode handle concurrent events?

Post by KimD » Fri Oct 23, 2015 11:32 pm

Hmmm. I'm using Windows 7, and I suspect that your example is more powerful than you had intended ;-) Both variants caused me to lose control of livecode entirely. I had to do an alt-ctrl-del and kill the livecode process to regain control.

After some experimentation I changed the "exit runCounter" to "exit to top", and also added an "exit to top" statement onto the second button. With these two modifications I was able to run the code and get what I think was the intended result (without losing control of livecode).
- Variant with wait only - option key is the only way out (I used the alt key as I don't have an option key)
- Variant with wait with message - both button 2 and option key provide a way out

But I think that I "get it". Using "wait with messages" provides a means to suspend the processing of an event and check to see if any other events have occurred. I suspect that the next lesson will be "use this superpower wisely or you can get yourself in a heck of a mess" ;-)

Thanks

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8421
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How does Livecode handle concurrent events?

Post by FourthWorld » Sat Oct 24, 2015 2:34 am

If you're feeling adventurous you can run this in the Message Box:

Code: Select all

go url "http://fourthworld.net/channels/lc/IdleHour.rev"
That'll download and run a simple example stack of simulating threads with wait and timers.
Richard Gaskin
LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”