Cocoa timer - Using NSTimer or an alternative for timer program
Cocoa timer - Using NSTimer or an alternative for timer program
- Subject: Cocoa timer - Using NSTimer or an alternative for timer program
- From: Alphonsus Chong <email@hidden>
- Date: Tue, 13 Dec 2005 20:55:03 -0800
Hi,
I'm a newbie to Cocoa trying to write a simple timer program. As a
prototype, I used NSTimer to 'tick' at 1 second intervals and that
seemed to work fine. However, when I proceeded to get it to fire at
0.01s, it was wildly inaccurate. A search of the Cocoa documentation
showed that NSTimer has a resolution of 50-100ms.
Looking around for alternatives, Cocoadev (
http://www.cocoadev.com/
index.pl?AnimationTimingAndCocoaDiscussion) has a article suggesting
the use of NSDate and NSTimeInterval. I tried to write a simple timer
based on this. However, I have not been able to get the timer to
display correctly. The time is only displayed after the timer has
ended and not after each iteration of the while loop. I am pretty
sure it is timing right and elapsed has the right value as NSLog is
showing the appropriate output.
My test code is as follows:
@interface MyObject : NSObject
{
IBOutlet NSTextField *textField;
}
- (IBAction)startTimer:(id)sender
{
NSDate *start;
NSTimeInterval elapsed;
elapsed = 0.0;
start = [NSDate date];
while (elapsed < 3.0){
elapsed = [start timeIntervalSinceNow] * -1.0;
[textField setDoubleValue:elapsed];
NSLog(@"elapsed time%f", elapsed);
}
}
I have been cracking my head over this and am pretty sure the answer
is pretty simple, but I still can't find it. I would appreciate it if
someone could point me to the right direction or suggest a good
method to get a timer working with 0.01 second accuracy.
I also read in the same Cocoadev article that runnning NSTimer in a
separate thread would give it a better resolution. I wonder if anyone
could share their opinion/ experience with that, and whether it would
be a good solution.
Thanks in advance.
_______________________________________________
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