Multi-threading a button action?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Multi-threading a button action?
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
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
Re: Multi-threading a button action?
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?
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?
Re: Multi-threading a button action?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10044
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Multi-threading a button action?
If I understand this correctly, threading wouldn't solve that problem but merely spawn new threads each time the space bar is pressed.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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Multi-threading a button action?
I grab the space bar with the following code in the card script:
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
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
Shawn
Re: Multi-threading a button action?
"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
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
Re: Multi-threading a button action?
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
"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
Re: Multi-threading a button action?
Something you could try is in the last line of the mouseUp in your button script:
... and add the handler:
... 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
Code: Select all
send "StartReading" to me in 10 milliseconds
Code: Select all
on StartReading
global isreading
put 0 into isreading
end StartReading
Regards,
Michael
Re: Multi-threading a button action?
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
in your keydown handler, after the message is sent:
put flushEvents("keydown") into temp
Craig Newman
Re: Multi-threading a button action?
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: Multi-threading a button action?
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
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
Re: Multi-threading a button action?
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
"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
-
- VIP Livecode Opensource Backer
- Posts: 10044
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Multi-threading a button action?
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Multi-threading a button action?
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: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
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
regards
Bernd
Re: Multi-threading a button action?
Exactly, Bernd, that's why I'm interested in seeing Shawn's script. Maybe he is doing more than just speaking.
Mark
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode