How does Livecode handle concurrent events?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How does Livecode handle concurrent events?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How does Livecode handle concurrent events?
#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.
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 development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How does Livecode handle concurrent events?
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?
And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How does Livecode handle concurrent events?
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.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
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.And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9669
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How does Livecode handle concurrent 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:And is it "first come first processed", or can some types of new event interrupt the processing of earlier events?
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
Code: Select all
on mouseUp
answer "Whew! I am outta here"
end mouseUp
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
Re: How does Livecode handle concurrent events?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How does Livecode handle concurrent events?
If you're feeling adventurous you can run this in the Message Box:
That'll download and run a simple example stack of simulating threads with wait and timers.
Code: Select all
go url "http://fourthworld.net/channels/lc/IdleHour.rev"
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn