1

Possible Duplicate:
NSTimer doesn't stop

I am using [NSThread exit] to leave the NSTimer thread. It stops the application entirely.

How do you stop or exit NSTimer thread?

Community
  • 1
  • 1
Woof
  • 699
  • 1
  • 5
  • 9
  • 2
    Timers don't run in their own thread, they run in the same thread. When the event loop reiterates it checks to see if any of the timers should be fired, and if they should be, the runloop fires them, but it all happens in the same thread. – dreamlax Jan 10 '12 at 01:56

4 Answers4

3
[timer invalidate];
timer = nil;
jdl
  • 6,151
  • 19
  • 83
  • 132
2

If you want to definitely stop a NSTimer you may use

[timer invalidate];

However, if you want to temporary prevent the timer to fire you may consider using

[timer setFireDate:[NSDate distantFuture]];

From Apple Documentation:

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes and releases the timer, either just before the invalidate method returns or at some later point. Once invalidated, timer objects cannot be reused.

Manlio
  • 10,768
  • 9
  • 50
  • 79
2

You should double check to make sure your timer is valid before invalidating it.

if ([timer isValid]) {
    [timer invalidate];
    timer = nil;
}
Keller
  • 17,051
  • 8
  • 55
  • 72
0

I am not sure what you mean by NSTimer thread, but to stop NSTimer instance you scheduled, you can call [NSTimer invalidate].

barley
  • 4,403
  • 25
  • 28