Cocoa cancel operation/method

Discussion in 'Mac Programming' started by wesg, Dec 1, 2008.

  1. wesg macrumors regular


    Jan 2, 2008
    Toronto, ON
    I'm writing a Cocoa Obj-C application that has a single operation. It will work through a loop (for or while) in a single window.

    I've gotten the interface working, but I'm stuck as to how best to implement a "cancel operation" button. What sort of code would I need to cancel the method that is started by the "DO work" button? Is there a single cancel operation that I can implement?
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Some semi psuedo code.

    while(doWork == true)
         // do stuff
        if(cancelButton == true)
              doWork = false;
  3. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    That would only work if you're doing the work on a background thread, since it'll never receive the cancel event if you're blocking the UI. If you aren't threading, you'd probably want to divide the work into bite-sized chunks and set a 0 delay timer to execute the next chunk each time one finishes so that the runloop has a chance to process events. Or use NSOperationQueue (which is equivalent to the background thread idea, but maybe easier to manage).

Share This Page