Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

eddjc

macrumors newbie
Original poster
Apr 12, 2007
7
0
Hello all,

Hope this is the right place to ask (if there is a better development forum please let me know!)

I've got a basic "tick" program that uses an NSTimer, currently running it in a "foundation" app (on the terminal). For some reason NSTimer doesn't seem to be firing:
Code:
#import <Foundation/Foundation.h>

@interface ECMetronome : NSObject
{
    NSTimeInterval millisecondsBetweenTicks;
    NSTimer *timer;
    NSNotificationCenter *notificationCenter;
    NSInvocation *invocation;
    
    
}

@property (nonatomic, retain) NSTimer * timer;
- (id) initWithMilliseconds: (double) milliseconds;
- (id) initWithMetronomeMark: (double) mark;

- (void) start;
- (void) tick: (NSTimer *)theTimer;

@end


Code:
#import "ECMetronome.h"

@implementation ECMetronome
@synthesize timer;

- (id)init
{
    self = [super init];
    if (self) {

        if (!millisecondsBetweenTicks) millisecondsBetweenTicks = 1000;

        
        NSLog(@"Metronome Initialised - metronome mark:%f", millisecondsBetweenTicks/1000*60);
        
    }
    
    return self;
}

- (id) initWithMilliseconds: (double) milliseconds
{
    millisecondsBetweenTicks = milliseconds; 
    return [self init];
}

- (void) setMetronomeMark:(double)mark
{
    millisecondsBetweenTicks = 60000/mark;
}

- (id) initWithMetronomeMark: (double) mark
{
    millisecondsBetweenTicks = 60000/mark;
    return [self init];
}


- (void) start
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:(millisecondsBetweenTicks/1000) target:self selector:@selector(tick:) userInfo:nil repeats:YES];
    NSLog(@"Timer Started");
}


- (void) tick: (NSTimer *)theTimer {
    
    NSLog(@"Tick!");
}

@end

Output is:
Code:
2012-07-27 01:56:07.573 commandLineMetronome[7396:303] Hello, World!
2012-07-27 01:56:07.575 commandLineMetronome[7396:303] Metronome Initialised - metronome mark:60.000000
2012-07-27 01:56:07.575 commandLineMetronome[7396:303] Timer Started

and then it stops running. Any ideas?

Edd
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Do you have a runloop at all?
Any event processing?

Did your program just exit?

Without a runloop, timers won't do anything. And when the program exits, timers will obviously not be called anymore. Timers don't keep your program alive.
 

eddjc

macrumors newbie
Original poster
Apr 12, 2007
7
0
Ahh I did see something about that in the Class Reference but didn't quite understand how it related/what I should do - Do I need to attach the timer to the run-loop in some way? Presumably if they're running on the same thread then they need to be at least aware of each other... (yes the program just exited)

Thanks
Edd

----------

Concat - thanks for the tip! Will use it.

Edd :)
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
I am just wondering something and it could just be me. Your Method

Code:
- (void) tick: (NSTimer *)theTimer {
    
    NSLog(@"Tick!");
}

excepts an argument theTimer of type NSTimer. But the timer code that executes your repeat timer sends no argument to that Method that I could see? If I were writing that method I would just have -(void) tick{}. Is your intention to pass some parameter at some point to this method?
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
I am just wondering something and it could just be me. Your Method

Code:
- (void) tick: (NSTimer *)theTimer {
    
    NSLog(@"Tick!");
}

excepts an argument theTimer of type NSTimer. But the timer code that executes your repeat timer sends no argument to that Method that I could see? If I were writing that method I would just have -(void) tick{}. Is your intention to pass some parameter at some point to this method?

No, the timer code in itself is right. scheduledTimerWithTimeInterval: repeat:YES will create a timer that sends the message to the target at the given interval, until the app quits or the timer is invalidated. HOWEVER this only works if you have a run loop running.

I suspect that the OP hasn't started with a "Cocoa application" project where you automatically have a runloop running. Without a run loop, you can do things like running tools, which do some task and then exit, but you can't have any useful user interface. And timers don't work.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.