How to quit NSThread

I use such a stream,

[NSThread detachNewThreadSelector:@selector(myfunction) toTarget:self withObject

the thread is working correctly, I want to exit the thread in the middle, how can I do this. If I use [NSThread exit], the application hangs.

+3
source share
2 answers

What thread do you work on "[NSThread exit]"? [NSThread output] runs on the current thread , so you need to call this as part of the myfunction selector. If you call it in the main thread, it will just exit the main thread.

In addition, it is not recommended to stop such streams, as this prevents the stream from exiting from cleaning resources.

myfunction .

- (void) myFunction
{
    while([someObject stillWorkToBeDone]) 
    { 
      performBitsOfWork();
    }
}

, "withObject". , , .

, smth, :

[someObject setStillWorkToBeDone:false];
+4

-[NSThread cancel]

-[NSThread isCancelled]

while.

+3

Source: https://habr.com/ru/post/1709067/


All Articles