3

I am using a thread to update messages in background in my application. The thread is started in my messages class.

Messages.m

timerThread = [[NSThread alloc] initWithTarget:self
                selector:@selector(startTimerThread:) object:nil]; 
[timerThread start];

now I want the thread to stop when the user signout of the application. For that in the signout method (in another class) I added

Messages *msg=[[Messages alloc]init];  
[msg.timerThread cancel];  
[msg release];  

But even after signing out of the application the thread is still running.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
sujith1406
  • 2,822
  • 6
  • 40
  • 60

4 Answers4

4

[timerThread cancel] doesn't stop thread, it just marks it as cancelled. I presume that your startTimerThread: performs some sort of infinite loop. You have to check isCancelled in this loop periodically and if it's YES you should perform some clean up and break from this loop.

BTW if you don't perform updates frequently it is more convenient to run NSTimer in main thread and detach new thread on callbacks (like [self performSelectorInBackground:@selector(updateMessages:) withObject:whatever]) or start an instance of NSOperation.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
hoha
  • 4,418
  • 17
  • 15
  • ok that means to set a global flag to indicate if app is signed out and if this flag is set ,break out of the loop right? – sujith1406 Mar 04 '11 at 13:41
  • There is no need in global flag. When you call [thread cancel] it's property isCanceled is set to YES. In your thread main cycle you can setup a simple check `if ([self isCancelled]) {break;}`. – hoha Mar 04 '11 at 14:47
2

( I am assuming here that you create an instance of Messages somewhere else in the program )

According to your provided code you are creating a new Messages instance as part of your sign out process ( which starts a new thread ), canceling that new thread ( -cancel is a proper way to stop a thread -- if your thread method follows the considerations listed in the documentation ), then releasing your ( extra? ) Messages instance. At this point your original Messages instance is still around, and the thread from that is still running.

If you want the thread to stop when the instance of the class is deallocated, you probably should take care of that in a -dealloc method on Messages and make sure your original Messages instance is released at the proper time.

Dre
  • 4,298
  • 30
  • 39
  • if i stop the thread in dealloc method of messages class i am stopping the update process as soon as i am getting out of the message screen but i want to stop the thread only when i am signing out of the application(which is handled by another view controller called settings.but how do i get a reference to the nsthread of messages class from here? – sujith1406 Mar 04 '11 at 13:10
  • You will need to move the update thread code to another class if you dealloc your messages class and still want to keep the thread ( which may or may not call methods or access data on the deallocated object ) running. Having a method still running code on a deallocated object is just asking for crashes. – Dre Mar 04 '11 at 13:14
1
[NSThread exit];

Have you checked this method of NSThread class.

Vaibhav Tekam
  • 2,344
  • 3
  • 18
  • 27
  • see if i am using [nsthread exit] in the signout class how i make sure it is referring to the nsthread started in the messages class itself – sujith1406 Mar 04 '11 at 12:59
0

Use [NSThread exit]; or [NSThread cancel];

visakh7
  • 26,380
  • 8
  • 55
  • 69