iphone - cancel a background thread in Objective-C -


how cancel background thread in objective-c?

i calling background threads so:

[self performselectorinbackground:@selector(setupthumbnails) withobject:nil]; 

it called when swipe happens, each swipe want cancel previous background thread request.

maintain cancellation property thread, , check periodically within thread. you'll need either use lock or atomic function (as provided operating system , frameworks, not implemented yourself!) indicate thread should cancelled. system-provided atomic functions documented in atomic manpage.

even better use nsoperation on nsoperationqueue instead of nsthread. still perform work in background using thread, lets framework , operating system manage thread pool on behalf, , nsoperation has iscancelled property can check - ability cancel either individual nsoperation or operations in entire nsoperationqueue.

for example, have "set 1 thumbnail" operation, , add 1 such operation queue each thumbnail set up. os , framework cooperate manage number of threads used execute operations, though set queue's width ensure @ many imports run @ once. , if user cancels, can ask queue cancel outstanding operations.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -