Timers will cause a runloop to fire (depending on run mode of course)
without any other source having to fire.

#import <Foundation/Foundation.h>

@interface TimerTest : NSObject
@end

@implementation TimerTest
- (void)runTest {
    NSLog(@"Test Thread Running");

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    [NSTimer scheduledTimerWithTimeInterval:5.0
                                     target:self
                                   selector:@selector(timerFired:)
                                   userInfo:nil
                                    repeats:YES];

    [[NSRunLoop currentRunLoop] run]; // will sit here until app termination

    [pool release];
}

- (void) timerFired:(NSTimer*)timer {
    NSLog(@"Timer Fired - %@", timer);
}
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"Test Starting");

    TimerTest* tt = [[TimerTest alloc] init];

    [NSThread detachNewThreadSelector:@selector(runTest)
                             toTarget:tt
                           withObject:nil];

    sleep(60);

    NSLog(@"Test Completed");

    [pool release];
}

----

run
[Switching to process 61896]
2010-11-17 13:59:19.329 TimerTest[61896:a0f] Test Starting
2010-11-17 13:59:19.332 TimerTest[61896:1503] Test Thread Running
Running…
2010-11-17 13:59:24.333 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:29.333 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:34.333 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:39.333 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:44.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:49.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:54.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 13:59:59.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 14:00:04.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 14:00:09.334 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 14:00:14.335 TimerTest[61896:1503] Timer Fired -
<NSCFTimer: 0x100300780>
2010-11-17 14:00:19.334 TimerTest[61896:a0f] Test Completed
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to