Dragon
Dragon

Reputation:

Exit NSThread

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.

Upvotes: 1

Views: 1788

Answers (2)

James Bush
James Bush

Reputation: 1527

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.

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299345

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.

Upvotes: 6

Related Questions