Re: NSTimer
Re: NSTimer
- Subject: Re: NSTimer
- From: "Adam R. Maxwell" <email@hidden>
- Date: Thu, 04 Dec 2008 21:11:26 -0800
On Dec 5, 2008, at 10:26 AM, Mahaboob wrote:
On 12/4/08 9:41 PM, "Adam R. Maxwell" <email@hidden> wrote:
On Dec 4, 2008, at 4:47 PM, Mahaboob wrote:
Hi,
I need to fire the thread repeatedly in regular time intervals. So I
used
the timer like:
timer = [[NSTimer scheduledTimerWithTimeInterval:(double)
[txtTimeinterval
doubleValue] target:self selector:@selector(myThread:) userInfo:nil
repeats:YES]retain];
It is working but, the thread is fired only after the the time
interval I
had given. But actually I need to begin the thread in 0Sec and then
keep the
regular time interval. For that I used the code :
NSDate* theDate = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
[[timer initWithFireDate:theDate interval:(double)
[txtTimeinterval
doubleValue] target:self selector:@selector(myThread:) userInfo:nil
repeats:YES]retain];
But it is not firing the thread.
Where I got wrong?
at least the following:
1) you appear to be misusing the initializer; you should have timer =
[[NSTimer alloc] initWithFireDate...];
2) you should be using theDate = [NSDate
dateWithTimeIntervalSinceNow:0]
3) you haven't added the timer to a runloop, so it's never scheduled,
and will never fire
Now I modified the code and attaching it.
Now thread fires only once. After that it enter into runLoop and not
going
further.
Why are you surprised that it fires only once, when all your timer
callback does is set a stop flag and stop the runloop (which is a bad
practice)?
In addition, you're running the runloop with a zero timeout (after you
stop it!) so you'd probably get one pass through it anyway. Your
timer callback also has the wrong argument type.
My recommendation is that you spend some time reading the runloop and
timer docs, at minimum, rather than pasting random pieces of code
together and hoping something works.
When I'm calling [self myThread:nil]; getting the warning:
'SendWindowClass' may not respond to '-send:'
(Messages without a matching method signature will be assumed to
return 'id'
and accept '...' as arguments.)
Without calling this the thread is not firing at all.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Re: NSTimer (From: Mahaboob <email@hidden>) |