Re: NSTimer question: How to fire at second 00, 05, 10 etc ?
Re: NSTimer question: How to fire at second 00, 05, 10 etc ?
- Subject: Re: NSTimer question: How to fire at second 00, 05, 10 etc ?
- From: Chris Kane <email@hidden>
- Date: Thu, 26 Oct 2006 14:56:14 -0700
On Oct 26, 2006, at 2:46 AM, Gilles Celli wrote:
Just use one of the variants that takes a start date for the first
firing, and compute the first fire date by taking the current time
interval (since reference date), add small fudge factor (so it's
not "too soon" in the future), round up to the next multiple of
5.0, and make a date from that. Use that as the first fire date
and an interval of 5.0.
Thanks for the reply...but actually (as a newbie Cocoa-programmer) I
tried it but couldn't figure it out (using NSDate ?)...if you have
an example it would be great.
Here's one: Attachment:
testti.m
Description: Binary data
This particular example uses a non-periodic timer to correct for
skews and drifts due to NTP, computational error, and other factors.
I changed it to fire at multiples of 5 seconds.
Or what are the advantages to use NSTimer instad of checking the
internal time and launch a requested method (it's what we do
actually with
our C-based UNIX program in the Terminal).
Well you didn't tell the group what the context was, so we couldn't
say anything about that. What are you doing when you're not checking
the time or acquiring the data, for example?
If you have a UI app (since you mention Cocoa), you should use an
NSTimer, and that will allow the UI to stay responsive to user input
events.
You could also spawn off a thread do a while (1) {sleep(5);
collect_data();} loop in it, but if you've never used threads before,
don't do that. Or find a book on threads to read first.
Chris Kane
Cocoa Framework, Apple
On Oct 25, 2006, at 8:30 PM, Chris Kane wrote:
On Oct 25, 2006, at 8:39 AM, Gilles Celli wrote:
Hi,
I've a question about NSTimer:
I've already set up a Cocoa program to get data from a Multimeter
every 5 seconds but what I really want
is to acquire data at second: 00, 05, 10, 15, 20, etc.
Any ideas ?
Just use one of the variants that takes a start date for the first
firing, and compute the first fire date by taking the current time
interval (since reference date), add small fudge factor (so it's
not "too soon" in the future), round up to the next multiple of
5.0, and make a date from that. Use that as the first fire date
and an interval of 5.0.
And has anyone any idea about the accuracy (in milliseconds) of
NSTimer ?
Are there any alternatives ?
Well if the program or system is busy doing something else when the
fire date comes around, the firing will be delayed until the timer
can be serviced by the run loop (in the program) and the thread can
get onto a processor. Also keep in mind that the computer's clock
is a simulation of actual time: the computer has to guesstimate how
long 5 seconds actually is from its internal interval timers/etc,
and outside forces like NTP can be adjusting the clock to make it
change faster or slower than "normal". For example, the computer
can think that 5.0 seconds have passed when only 4.9 seconds
(measured by an accurate outside clock) have passed. Those extra-
NSTimer effects could end up being more interesting.
Otherwise, just write a little test program that just prints the
current time (probably as the reference time interval double, not
as a date object, for more accuracy) in a method fired by an
NSTimer, and see what happens.
Chris Kane
Cocoa Frameworks, Apple
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden