Re: Timers in a Foundation Tool
Re: Timers in a Foundation Tool
- Subject: Re: Timers in a Foundation Tool
- From: David Remahl <email@hidden>
- Date: Fri, 4 Jul 2003 23:50:13 +0200
While Jeff is perfectly correct (timers require a running run loop),
there is another problem in the code below.
For the timer action selector, you pass a function pointer. It is
important to realize that functions != methods. Selectors are
associated with a class or an object, and timers must have a target.
This is an example of a working program:
#import <Foundation/Foundation.h>
@interface SomeClass : NSObject
- (void)triggerTimer:(id)timer;
@end
@implementation SomeClass
- (void)triggerTimer:(id)timer {
NSLog(@"Hello again...");
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
SomeClass *someObj = [[SomeClass alloc] init];
// insert code here...
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2
target:someObj selector:@selector(triggerTimer:) userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate
dateWithTimeIntervalSinceNow:1000.0]];
[pool release];
return 0;
}
/ Regards, David
On fredag 4 juli 2003, at 23.06PM, Jeff Harrell wrote:
NSRunLoop!
I ran into a similar problem recently with a threaded AppKit
application. I'm not sure it's the case with this application, but I'm
willing to suggest it anyway. You may need to manually start a run
loop before your thread will fire.
[[NSRunLoop currentRunLoop] run];
should do the job. The +currentRunLoop method creates a run loop is
none yet exists.
On Friday, July 4, 2003, at 03:52 PM, Brian Ganninger wrote:
Dear List,
Sorry if this is a repeated question, but after searching the archives
for 2 hours I give. I have a foundation tool that I want to leave
running with a timer that fires every minute. Below is the non-working
test code I strapped together. I can't figure out how to target the
timer so it'll call the launchEngine companion method.
Any input is greatly appreciated.
Sincerely,
Brian Ganninger
#import <Foundation/Foundation.h>
void launchEngine()
{
NSLog(@"Hello again...");
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2
target:nil selector:launchEngine() userInfo:nil repeats:TRUE];
sleep(1000);
[pool release];
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.