1

how to exit thread while its in running mode...when i use NSThread exit my app get hanged... Can any one help me ? what could i use here to exit thread or close thread

Thanks Its my first post here.

Marc W
  • 19,083
  • 4
  • 59
  • 71

2 Answers2

6

The routine you're looking for is return.

The thread will terminate when the function that you launched it with finishes. You don't need to terminate the NSThread; it will handle that itself. You just need to return from the function call.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

I assume you're looking for a way to cancel a thread once you've started it. It's all in the set up. Here's an example:

  • (

    void) doCalculation{ /* Do your calculation here */ }

    • (void) calculationThreadEntry{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSUInteger counter = 0; while ([[NSThread currentThread] isCancelled] == NO){ [self doCalculation]; counter++; if (counter >= 1000){ break; } } [pool release]; } application:(UIApplication *)application
    • (BOOL) didFinishLaunchingWithOptions:(NSDictionary )launchOptions{ / Start the thread */ [NSThread detachNewThreadSelector:@selector(calculationThreadEntry) toTarget:self withObject:nil]; // Override point for customization after application launch. [self.window makeKeyAndVisible]; return YES; }

In this example, the loop is conditioned on the thread being in a non-cancelled state.

James Bush
  • 1,485
  • 14
  • 19