What is the best way to implement Wait Mode?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
What is the best way to implement Wait Mode?
I am writing a children's app. When the child clicks (touches) one of the images on the screen (i.e., the transparent buttons on the images), things happen. When there is no activity from the child for a time, the program prompts by blinking each of the clickable objects in sequence. The way I have programmed it is as follows:
The main loop is:
on mouseUp
respondToTheClick
waitMode
end mouseUp
and the wait loop is:
on waitMode
blinkIfACertainTimeHasElapsed
if the mouseClick then exit waitMode
end waitMode
As you can see, the program is stuck in one mouseUp handler until the next mouseClick. I worry if this could lead to the program hanging. My question is, is there a standard way of implementing the wait mode? (As you can tell, I don't have a background in programming).
Thanks for your time,
Sri.
The main loop is:
on mouseUp
respondToTheClick
waitMode
end mouseUp
and the wait loop is:
on waitMode
blinkIfACertainTimeHasElapsed
if the mouseClick then exit waitMode
end waitMode
As you can see, the program is stuck in one mouseUp handler until the next mouseClick. I worry if this could lead to the program hanging. My question is, is there a standard way of implementing the wait mode? (As you can tell, I don't have a background in programming).
Thanks for your time,
Sri.
Re: What is the best way to implement Wait Mode?
Use the "send" command, with its "in time" variant. This is much more robust than using wait, as it frees the system for other tasks. Never use "wait" unless absolutely required.
on mouseUp
set the noClick of this stack to "" --see below
waitMode
end mouseUp
on waitMode
respondToTheClick --need to set some sort of flag here. See below
doYourBlinkThing --but no waiting anymore, just blink as you think fit
If the noClick of this stack = "" then send "waitMode" to this stack in blinkTime seconds -- BlinkTime is your blink interval. Send "waitMode" wherever you do now,
end waitMode
Now, you have to be able to get out of the successive "waitmode" messages. Perhaps set a custom property (noClick) upon a successful "respondToTheClick", so that the conditional will not fire.Maybe set it to "Clicked" if there was one, and of course empty if not.
You no longer have to exit with a mouseClick, as the handler will simply end if it sees that a click was made.
Untested, but should get you going. Untested means there are conceptual bugs.
Craig Newman
on mouseUp
set the noClick of this stack to "" --see below
waitMode
end mouseUp
on waitMode
respondToTheClick --need to set some sort of flag here. See below
doYourBlinkThing --but no waiting anymore, just blink as you think fit
If the noClick of this stack = "" then send "waitMode" to this stack in blinkTime seconds -- BlinkTime is your blink interval. Send "waitMode" wherever you do now,
end waitMode
Now, you have to be able to get out of the successive "waitmode" messages. Perhaps set a custom property (noClick) upon a successful "respondToTheClick", so that the conditional will not fire.Maybe set it to "Clicked" if there was one, and of course empty if not.
You no longer have to exit with a mouseClick, as the handler will simply end if it sees that a click was made.
Untested, but should get you going. Untested means there are conceptual bugs.
Craig Newman
Re: What is the best way to implement Wait Mode?
Hi Sri
As faster Graig said you need a "send" command.
This script (for a card script) do an answer dialog after 10 seconds without activity. In this case i consider the mousemove event like an activity, but you can use an other event.
Sorry for my english if i'm not clear
Best regards
Jean-Marc
As faster Graig said you need a "send" command.
This script (for a card script) do an answer dialog after 10 seconds without activity. In this case i consider the mousemove event like an activity, but you can use an other event.
Sorry for my english if i'm not clear
Best regards
Jean-Marc
Code: Select all
local sDepTime,sDuration
on opencard
put the seconds into sDepTime
put 10 into sDuration
ShtempsPasse
end opencard
on closecard
finShtempsPasse
end closecard
on mousemove
put the seconds into sDepTime
end mousemove
on ShtempsPasse
if the seconds > sDepTime + sDuration then
answer "Time is over" -- or what you want
put the seconds into sDepTime
end if
send ShtempsPasse to me in 1000 milliseconds
end ShtempsPasse
on finShtempsPasse -- stop the pendinmessage
repeat for each line aLine in the pendingMessages
if aLine contains "ShtempsPasse" then
cancel item 1 of aLine
end if
end repeat
end finShtempsPasse
https://alternatic.ch
Re: What is the best way to implement Wait Mode?
Hi Craig and Jean-Marc:
Thank you both for your responses.
Craig, your code
I agree with you about avoiding the use of the "wait" command. Here's a short piece of code incorporating the "send" command:
Jean-Marc,
thanks for your code. I have a question. You are cleaning up the message cache on closeCard. Why is it necessary? Wouldn't all the card messages running automatically terminate and message cache empty when you close a card?
Thanks,
Sri.
Thank you both for your responses.
Craig, your code
checks the condition of no click and then waits, say, 30 seconds before sending the waitMode message to the stack; what happens if there is a click in those 30 seconds? The noClick would have updated, but IF clause won't check it again, so the code won't work (or am I wrong here?).If the noClick of this stack = "" then send "waitMode" to this stack in blinkTime seconds
I agree with you about avoiding the use of the "wait" command. Here's a short piece of code incorporating the "send" command:
Code: Select all
on waitMode
if the seconds - gLastClickTime > 30 then -- if more than 30 seconds since the last click
repeat with i = 1 to the number of buttons of this card
blinkObject button i of this card -- blinks each card once
if the mouseClick is true then exit waitMode -- if user clicked anytime, stop serial blinking and respond
end repeat
put the seconds into gLastClickTime -- reset the clock
end if
if the mouseClick is true then exit waitMode -- this checks for clicks between blinking episodes
send "waitMode" to this stack
end waitMode
thanks for your code. I have a question. You are cleaning up the message cache on closeCard. Why is it necessary? Wouldn't all the card messages running automatically terminate and message cache empty when you close a card?
Thanks,
Sri.
Re: What is the best way to implement Wait Mode?
Hi
I'm not sur it is necessary.You are cleaning up the message cache on closeCard. Why is it necessary?
Welcome. I try to give what i have received from this forumThanks for your time
https://alternatic.ch
Re: What is the best way to implement Wait Mode?
I don't know if this will help or not, but I've got something like this in my app.
Of course I made a few modifications to reflect you situation.
Basically you've got two global fields" sec.cntr & timer.on.
When you want to start waiting, you put true in timer.on,
and then call on.blinktimer. Then after 60 seconds,
you simply pass the image.name, where you use a single
function to set the visible on and off, to the blinking,
for any image, as need be.
Of course I made a few modifications to reflect you situation.
Basically you've got two global fields" sec.cntr & timer.on.
When you want to start waiting, you put true in timer.on,
and then call on.blinktimer. Then after 60 seconds,
you simply pass the image.name, where you use a single
function to set the visible on and off, to the blinking,
for any image, as need be.
Code: Select all
do blink.timer
global sec.cntr, timer.on
add 1 to sec.cntr
if sec.cntr >= 60 then --wait 60 seconds before blinking
put zero into sec.cntr
call start.blinking(image.name)
end if
if timer.on then
send "blink.timer" to me in 1 second
end if
end blink.timer
Last edited by townsend on Fri Jun 29, 2012 12:51 am, edited 1 time in total.
Re: What is the best way to implement Wait Mode?
Hi Townsend:
It is not just waiting, but waiting while there is no click. So, the routine has to exit when there is a click.
In any case, in the code I have given above, I, too, have used a global variable as timer. It seems to work.
Regards,
Sri.
It is not just waiting, but waiting while there is no click. So, the routine has to exit when there is a click.
In any case, in the code I have given above, I, too, have used a global variable as timer. It seems to work.
Regards,
Sri.
Re: What is the best way to implement Wait Mode?
Quite.
Why not send the "waitMode" command in 0 seconds, and create a timer?
Conceptually, the "send in time" command creates a "repeat loop", but through an entire handler as a whole. If you pass the seconds as a parameter each time you send, and check that value with a preset time, you can send continuously. You can probably do this the other way around, sending the preset, and checking the seconds. The blink would only occur after the preset time was passed. In the clickChecking and blink handlers, you can reset the timer.
Untested, so guaranteed to have conceptual issues.
Craig Newman
Why not send the "waitMode" command in 0 seconds, and create a timer?
Conceptually, the "send in time" command creates a "repeat loop", but through an entire handler as a whole. If you pass the seconds as a parameter each time you send, and check that value with a preset time, you can send continuously. You can probably do this the other way around, sending the preset, and checking the seconds. The blink would only occur after the preset time was passed. In the clickChecking and blink handlers, you can reset the timer.
Untested, so guaranteed to have conceptual issues.
Craig Newman
Re: What is the best way to implement Wait Mode?
I extracted this from a gadget I made a while ago.
Untested, as usual. This should be considered pseudocode.
Craig Newman
Code: Select all
on waitMode baseTime
repeat until the seconds > baseTime + 5 --your value here
checkForClick
end repeat
doYourBlinkThing
put the seconds into baseTime
send "waitMode baseTime" to this stack in 0 millisec
Craig Newman