3

Lets say i am running some code in dispatch async. .. is there a way to terminate the thread it creates before it completes? like when the user clicks cancel

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    //start doing something here.. break bofore it finishes? 


    dispatch_async(dispatch_get_main_queue(), ^{

    //main thread stuff..   

    });



});
Jinah Adam
  • 1,125
  • 2
  • 14
  • 27
  • possible duplicate of [Dispatch queues: How to tell if they're running and how to stop them.](http://stackoverflow.com/questions/1550658/dispatch-queues-how-to-tell-if-theyre-running-and-how-to-stop-them) – David Gelhar Oct 26 '10 at 03:04
  • oh sorry about that. so in short i should just use a flag. – Jinah Adam Oct 26 '10 at 03:14
  • 1
    Yep, set a flag to request cancellation, and have your async operation check the cancellation status at designated points where it's safe to cancel (making sure to do any necessary cleanup). – David Gelhar Oct 26 '10 at 03:23
  • 1
    Click here for a full discussion .. http://stackoverflow.com/questions/5449469/can-you-use-cancel-iscancelled-with-gcd-dispatch-async – Fattie Mar 27 '11 at 20:53

1 Answers1

3

David is right. GCD has no built-in method of cancellation. It is up to the client (you).

Ryan
  • 16,626
  • 2
  • 23
  • 20