Using non blocking wait

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Using non blocking wait

Post by kolia » Sun Nov 22, 2015 7:55 pm

Hello,

I have a handler "updateStatus" that runs every 10 seconds. At the very beginning of this handler we have

Code: Select all

setUpdateIsRunning true
and at the end

Code: Select all

setUpdateIsRunning false
User may click on any interface control at any time. At the beginning of the control handler, we have to make sure that updateStatus is not running in order to avoid concurrent calls to a hardware device. I have this instruction :

Code: Select all

   wait while getUpdateStatusRunning() is true with messages 
Can anyone tell me that it does what I'm expecting, that is the control handler will resume execution when the updateStatus handler hits the end, in order to avoid possible concurrent calls?
Thank you

Just for reference the code of the two routines

Code: Select all

local updateStatusRunning
command setUpdateStatusRunning pValue
   put pValue into updateStatusRunning
end setUpdateStatusRunning

function getUpdateStatusRunning
   if updateStatusRunning is empty then put false into updateStatusRunning 
   return updateStatusRunning
end getUpdateStatusRunning
Last edited by kolia on Thu Nov 26, 2015 11:36 am, edited 1 time in total.
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

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

Re: Using wait

Post by dunbarx » Mon Nov 23, 2015 4:49 am

Hi.

It is hard to comment, because, for example, you do not say what or where the handler "setUpdateIsRunning" is, or what it does. If I tried to reconstruct the handful of code smippets you mentioned, I would get a runtime error. There is no such handler, and I cannot go any further.

Perhaps come back with complete handlers? I hope they are not too long. I generally do not read long handler submissions, unless I can extract the salient portions or rewrite them into shorter examples.

Craig Newman

kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Re: Using wait

Post by kolia » Thu Nov 26, 2015 10:48 am

Hi Craig,
Thanks for getting involved in my question. I'll try to be as concise as possible.

updateStatus runs every 10 sec. At the beginning of the routine it sets a flag (setUpdateStatusRunning true) and at the end it sets the same flag to false (setUpdateStatusRunning false)

Whenever a user click, at the beginning of the handler the status of the flag is checked and the handler is paused (non blocking) until the flag is set to false by updateStatus. The instruction at the beginning this handler is:

Code: Select all

wait while getUpdateStatusRunning() is true with messages.
Both handlers, updateStatus and onMouseUp, access a remote hardware device and I wan't to avoid concurrent accesses. So I wan't to make sure that "wait while getUpdateStatusRunning() is true with messages" will pause onMouseUp until updateStatus reaches the end.

Nicolas
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

WaltBrown
Posts: 466
Joined: Mon May 11, 2009 9:12 pm
Location: NE USA

Re: Using non blocking wait

Post by WaltBrown » Thu Nov 26, 2015 2:56 pm

What is the difference between "setUpdateStatusRunning" and "setUpdateIsRunning"? You gave the code for the former but not the latter. Typo?
Walt
Walt Brown
Omnis traductor traditor

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: Using non blocking wait

Post by zaxos » Thu Nov 26, 2015 7:43 pm

Code: Select all

wait while getUpdateStatusRunning() is true with messages.
No need for that, instead:
e.x. Code of button 1

Code: Select all

global isRunning
on mouseUp
if isRunning then
doSomething
else
-- do nothing
end if
end mouseUp
Code in stack

Code: Select all

global isRunning
on updateStatus
put true into isRunning
do stuff
do stuff
do stuff
do stuff
put false into isRunning
send isRunning to me in 10 sec
end updateStatus
You can use custom properties instead of a global, you can actualy have all the scripts in the card, but you get the big picture.
Knowledge is meant to be shared.

kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Re: Using non blocking wait

Post by kolia » Sat Nov 28, 2015 9:53 am

WaltBrown wrote:What is the difference between "setUpdateStatusRunning" and "setUpdateIsRunning"? You gave the code for the former but not the latter. Typo?
Walt
Yes sorry typo error the routine is setUpdateStatusRunning
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Re: Using non blocking wait

Post by kolia » Sat Nov 28, 2015 10:39 am

Thanks Xanos for your contribution. But this is not the idea. The idea is to have the onMouseUp handler resuming execution when the updateStatus reaches the end.
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3323
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Using non blocking wait

Post by bn » Sat Nov 28, 2015 11:44 am

Hi Nicolas,

as far as I see when you run your "update" handler there is no way for another message to get processed since you don't e.g. wait with messages in that handler.

In theory you should not even have to check if the update handler runs because once it runs it blocks all other requests. Once the update handler is finished mouseUp should fire because it should be queued.

Unless your "update" handler is non-blocking

I made a little stack that simulates both situations.

On top 3 buttons. Click "simulate non blocking update" (runs 5 seconds) then "my MouseUp" repeatedly, in the field you will see the status of the simulate handler with the milliseconds attached to see something is going on.
myMouseUp2 button has a handler that checks the status of the non-blocking button and only proceeds when the non-blocking buttonn is done.

at the bottom 2 buttons for a blocking update handler
click "simulate blocking update" button (the button just waits for 5 seconds) and then click the myMouseUP3 button. The button does not react but the action is queued. Once the blocking handler is done you see the milliseconds change in the field.

By the way when checking the non-blocking handler with button myMouseUp2 I first tried to use "wait 1 second with messages" in a repeat loop: That did not work, apparently the non-blocking handler, which also waited with messages then never ended waiting. I had to use "send in time" to do this.


Kind regards
Bernd
Attachments
blockMouseUpWhenBusy.livecode.zip
(1.67 KiB) Downloaded 50 times

kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Re: Using non blocking wait

Post by kolia » Sat Nov 28, 2015 4:20 pm

Hi Bernd
I really appreciate your valuable input. It explains another behavior I noticed without being able to understand it. This makes very clear how LC handles interruption if I may say so. I will check the stack you attached as soon as I can and I will post what I understood.
Thanks again
Nicolas
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

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

Re: Using non blocking wait

Post by FourthWorld » Sat Nov 28, 2015 7:21 pm

Only peripherally related, but this modest demonstration stack shows a couple ways to simulate some limited forms of threading in LC:
http://fourthworld.net/channels/lc/IdleHour.rev
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

kolia
Posts: 82
Joined: Thu May 28, 2015 3:53 pm

Re: Using non blocking wait

Post by kolia » Sun Nov 29, 2015 12:13 am

Thanks Richard, I'll look at this one as well
Nicolas
LC 7.1 on Windows 8.1 and on MAC OS 10.10.5 Xcode 6.4 - 7.1

Post Reply

Return to “Talking LiveCode”