Multi-threading a button action?

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

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 1:39 pm

Does RunRevolution support multi-threading?

I have an application that when a specific key (space) is pressed that in turn sends a message to a button. The button performs an action that takes several seconds. While that is occurring it appears that revolution is buffering the space key presses. When the buttons action completes the system then generates new messages to the button and the action repeats.

What I am trying to do is ignore any space keys while the button action is running. At first I tried setting a variable to 1 while the button action is occuring and then back to 0 when it finishes. In the cards script handler if that variable is a 1 (action occuring) on a space key I do not send the message to the button. This does not work because it appears Revolution is single threading the button action and then running the card script handler. By the time the card script handler is evaluating the keypress the action has completed and the variable is back to a 0 so it generates new messages.

The other approach I took is at the end of the button action trying to cancel any pending messages. This also does not work again I assume because the card script has not yet created any pending messages because Revolution is single threading the button action and the card script.

Is it possible to make Revolution perform the button action on a separate thread? Any other ideas on how to make this work?

Thanks,

Shawn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Multi-threading a button action?

Post by dunbarx » Wed Apr 07, 2010 2:52 pm

How are you detecting the spaceKey? With a rawKeyDown handler, I get what you are saying. There does seem to be a queue of pending messages sent.

With a RawKeyUp handler I only get a single message sent to a button that had a task in it. Maybe that is a solution? Can you post your script, or as small a version as possible that shows what is happening?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Multi-threading a button action?

Post by dunbarx » Wed Apr 07, 2010 3:02 pm

Shawn.

Another thought. On a mac at least, the time it takes for the additional messages to be sent are set by the "delay until repeat" setting. The time between repeat events by the "key repeat rate".

Rev is just responding to normal system keyboard actions. All these go away with "rawKeyUp".

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10044
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Multi-threading a button action?

Post by FourthWorld » Wed Apr 07, 2010 3:50 pm

shawn wrote:Does RunRevolution support multi-threading?

I have an application that when a specific key (space) is pressed that in turn sends a message to a button. The button performs an action that takes several seconds. While that is occurring it appears that revolution is buffering the space key presses. When the buttons action completes the system then generates new messages to the button and the action repeats.

What I am trying to do is ignore any space keys while the button action is running.
If I understand this correctly, threading wouldn't solve that problem but merely spawn new threads each time the space bar is pressed.

Either way, you'll want to prevent those actions from triggering your script. You might consider using either the flushEvents function, or perhaps simpler to set a flag when that action is performed which is checked at the start of the action - if the flag is set the action isn't performed.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Re: Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 3:51 pm

I grab the space bar with the following code in the card script:

Code: Select all

on keydown theKey
   Global isReading
   if isReading = 0 Then
      
      if TheKey = " " Then
         send mouseup to button singleswitch
      end if
      
     
   End If
   
   if TheKey <> " "  Then
         pass keydown
      End If


end keydown
The button when it receives the mouseup sets isReading to 1. When the button action completes it sets it back to 0. However, by the time keyDown runs again isReading is always 0 as execution isn't going back to the card script until the button action completes. It appears to be single threaded, how do I make it multi-threaded.

Shawn

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Re: Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 3:59 pm

"perhaps simpler to set a flag when that action is performed which is checked at the start of the action - if the flag is set the action isn't performed."

That is exactly what I am doing now, it doesn't work because of the single threading. The key is pressed, the button is trigger, flag is set, the action completes, the flag is reset and then the card script evaluates the next key press even if the keypress occured while the action was occuring. If the user hits the space bar again before the script completes the keypress is not evaluated until after the action is complete and the flag is reset.

"threading wouldn't solve that problem but merely spawn new threads each time the space bar is pressed."

Threading would completely solve the problem because the second key press would be evaluated while the flag was set (action occuring) and it would therefor be ignored. The way it works now is the keypress is not evaluated till after the action completes.

"You might consider using either the flushEvents function"

I will try that. At the end of the action I added:

repeat until the pendingMessages is empty

cancel item 1 of line 1 of the pendingMessages

end repeat


But that does not help either as their are no pending messages yet because they have not been evaluated yet due to the single threaded nature.

Shawn

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Re: Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 4:01 pm

Craig,

"Another thought. On a mac at least, the time it takes for the additional messages to be sent are set by the "delay until repeat" setting. The time between repeat events by the "key repeat rate"."

The additional space bar hits are not due to the key repeat of the OS, but from actual additional keypresses.

"Rev is just responding to normal system keyboard actions. All these go away with "rawKeyUp"."

I will try it but I doubt this will change anything as there will be more then one rawKeyUp message and I doubt Revolution is going to evaluate that while the flag is set.

Thanks,

Shawn

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Multi-threading a button action?

Post by Regulae » Wed Apr 07, 2010 5:07 pm

Something you could try is in the last line of the mouseUp in your button script:

Code: Select all

send "StartReading" to me in 10 milliseconds
... and add the handler:

Code: Select all

on StartReading
   global isreading
   put 0 into isreading
end StartReading
... to the button script, which may screen out the unwanted keypresses by using a slight delay after the mouseUp terminates. This seemed to work for my tests, but I may not have fully understood the problem.

Regards,

Michael

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Multi-threading a button action?

Post by dunbarx » Wed Apr 07, 2010 6:33 pm

As usual, Rev has what you need built-in.

in your keydown handler, after the message is sent:

put flushEvents("keydown") into temp

Craig Newman

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Multi-threading a button action?

Post by Mark » Wed Apr 07, 2010 6:39 pm

Hi Shawn,

I think that the actual problem is in the button script, which takes several seconds to complete. Would it be possible to change this script, to make it "multi-threaded", allowing for handling key strokes? Perhaps you could post your button script here?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Re: Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 7:51 pm

Craig,

You flush works but it is discarding all keypresses, I just want to ignore one specific keypress. For future use I'd also really need the ability to thread so I could respond to multiple keypresses and trigger multiple events that would run concurrently.

Shawn

shawn
Posts: 18
Joined: Sat Jan 02, 2010 4:11 am

Re: Multi-threading a button action?

Post by shawn » Wed Apr 07, 2010 7:58 pm

Mark,

"I think that the actual problem is in the button script, which takes several seconds to complete. Would it be possible to change this script, to make it "multi-threaded", allowing for handling key strokes? Perhaps you could post your button script here?"

The application is a screen reader for those with physical disabilities that use single switch for access. The button script is speaking X number of sentences of text when the switch (which is a space on the keyboard) is pressed. This takes at the very least a few seconds to a minute or more depending upon how many sentences it is set to read.

The idea is that if the use hits the switch while the app is speaking the text it will ignore it. Future apps would need the ability to handle multiple switches (different key presses) all of which could be active concurrently. The lack of threading is going to be a problem with this.

Shawn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10044
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Multi-threading a button action?

Post by FourthWorld » Wed Apr 07, 2010 8:40 pm

If you add "wait 0 with messages" in your repeat loop the engine will accept additional events, which may help with your flag check.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4166
Joined: Sun Jan 07, 2007 9:12 pm

Re: Multi-threading a button action?

Post by bn » Wed Apr 07, 2010 8:53 pm

shawn,
revspeak is non-blocking since Rev unloads the reading to the system.
Suppose you are using the keyDown message in a card script to start reading you could say this:

Code: Select all

on keyDown whichKey
   -- check for space
   if whichkey is space then
      -- check if it is reading now
      -- if so exit keyDown
      if  revIsSpeaking() then 
         exit keyDown
      end if
      -- read aloud field 1
      revspeak field 1
   end if
   pass keyDown
end keyDown
once you started the speach you can do whatever you want except starting a new speach with the space key since for every press of the space key the handler checks if "revIsSpeaking()"
regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Multi-threading a button action?

Post by Mark » Wed Apr 07, 2010 8:58 pm

Exactly, Bernd, that's why I'm interested in seeing Shawn's script. Maybe he is doing more than just speaking.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply