Re: Cocoa timer - Using NSTimer or an alternative for timer program
Re: Cocoa timer - Using NSTimer or an alternative for timer program
- Subject: Re: Cocoa timer - Using NSTimer or an alternative for timer program
- From: Eric Smith <email@hidden>
- Date: Tue, 13 Dec 2005 21:16:10 -0800
Your user interface is running on the same thread as the startTimer
code, so it's blocked while that method is running. The only way to
get the timer and user interface to operate in a way that seems
simultaneous (ie, the textview updates as the timer counts) is to run
them in different threads. As for the resolution of NSTimer... I
can't say. Although I do use it for animating images. 100ms
resolution would cause a jerky image, which I don't see, but for all
I know I might be getting only 50ms resolution out of it.
Eric
On Dec 13, 2005, at 8:55 PM, Alphonsus Chong wrote:
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:
40mac.com
This email sent to email@hidden
_______________________________________________
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