Page 1 of 1

Clock problem...

Posted: Wed Oct 03, 2012 12:10 pm
by paulsr
Greetings:

I'm developing an app which will, amongst other things, display a digital clock.

I have created six label fields which will display the six digits of hh:mm:ss using an LCD font.

(I am developing at the moment in Windows, altho the app is destined for iOS.)

I thought this would be fairly simple, and altho the flwg code works, there is a problem...

Code: Select all

command RunClock
   repeat
   put the long time into tTime
   replace ":" with " " in tTime
   put character 1 of word 1 of tTime into fld "LblCLKd5"
   put character 2 of word 1 of tTime into fld "LblCLKd4"
   put character 1 of word 2 of tTime into fld "LblCLKd3"
   put character 2 of word 2 of tTime into fld "LblCLKd2"
   put character 1 of word 3 of tTime into fld "LblCLKd1"
   put character 2 of word 3 of tTime into fld "LblCLKd0"
   wait 100 milliseconds with messages
   end repeat
end RunClock

on openCard
   RunClock
end openCard
When the clock is running, I can't do much in Livecode, like continue editing the app. The clock seems to take over the computer. If I type a few characters in the editor, everything freezes and I have to press ctrl'.' to stop the app.

Clearly I am doing something wrong. Is there a better way to handle clocks/timers that run constantly.

TIA for any help...

--paul

Re: Clock problem...

Posted: Wed Oct 03, 2012 12:29 pm
by Klaus
Hi Paul,

yep, using an endless reapt loop for these kind of things is a very bad idea, as you have experienced! 8)
Use "send... in..." like this:

Code: Select all

## See below...
local the_clock_message_id

command RunClock
   global the_clock_message_id
   put the long time into tTime
   replace ":" with " " in tTime
   put character 1 of word 1 of tTime into fld "LblCLKd5"
   put character 2 of word 1 of tTime into fld "LblCLKd4"
   put character 1 of word 2 of tTime into fld "LblCLKd3"
   put character 2 of word 2 of tTime into fld "LblCLKd2"
   put character 1 of word 3 of tTime into fld "LblCLKd1"
   put character 2 of word 3 of tTime into fld "LblCLKd0"
   send "RunClock" to me in 100 milliseconds
   put the result into the_clock_message_id
end RunClock

on openCard
   RunClock
end openCard

on closecard
  global the_clock_message_id
  cancel the_clock_message_id
 ## your stuff here...
end closecard
You need to cancel this "SENDING" when the card closes, so I added a local variable,
which will hold the ID of the sent message (check "pendingmessages" and "send" in the dictionary)
so you can CANCEL it when the card closes.

Best

Klaus

Re: Clock problem...

Posted: Wed Oct 03, 2012 12:44 pm
by paulsr
Awesome! Thanks Klaus.

--paul

Re: Clock problem...

Posted: Wed Oct 03, 2012 1:03 pm
by Klaus
Hi Paul,

a little error in my code, NO global of course in the "closecard" handler:

Code: Select all

on closecard
  cancel the_clock_message_id
  ## your stuff here...
end closecard
Best

Klaus