Quitting NSThread

how to exit a thread while it is running in runtime ... when I use NSThread exit my application gets hung ... Can anyone help me? what can i use here to exit the stream or close the stream

Thanks, my first post is here.

+1


a source to share


2 answers


The procedure you are looking for is return

.



The thread will stop when the function you started with termination. You don't need to stop NSThread; he can handle it himself. You just need to return from the function call.

+6


a source


I am assuming that you are looking for a way to cancel the thread as soon as you start it. It's all in customization. Here's an example:

  • (

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

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


In this example, the loop is due to the thread being in a non-canceled state.

0


a source







All Articles