Clock problem...

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

Post Reply
paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Clock problem...

Post by paulsr » Wed Oct 03, 2012 12:10 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Clock problem...

Post by Klaus » Wed Oct 03, 2012 12:29 pm

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

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Clock problem...

Post by paulsr » Wed Oct 03, 2012 12:44 pm

Awesome! Thanks Klaus.

--paul

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Clock problem...

Post by Klaus » Wed Oct 03, 2012 1:03 pm

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

Post Reply