Cocoa cancel operation/method

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

  1. wesg macrumors regular

    wesg

    Joined:
    Jan 2, 2008
    Location:
    Toronto, ON
    #1
    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

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Some semi psuedo code.

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

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    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