title: How do I update a progress bar in Cocoa during a long running loop?
body: <p>I've got a <code>while</code> loop, that runs for many seconds and that's why I want to update a progress bar (NSProgressIndicator) during that process, but it updates only once after the loop has finished. The same happens if I want to update a label text, by the way.</p>
  
  <p>I believe, my loop prevents other things of that application to happen. There must be another technique. Does this have to do with threads or something? Am I on the right track? Can someone please give me a simple example, how to “optimize” my application?</p>
  
  <p>My application is a Cocoa Application (Xcode 3.2.1) with these two methods in my <code>Example_AppDelegate.m</code>:</p>
  
  <pre>
  // This method runs when a start button is clicked.
  - (IBAction)startIt:(id)sender {
      [progressbar setDoubleValue:0.0];
      [progressbar startAnimation:sender];
      running = YES; // this is a instance variable
  
      int i = 0;
      while (running) {
          if (i++ >= processAmount) { // processAmount is something like 1000000
              running = NO;
              continue;
          }
  
          // Update progress bar
          double progr = (double)i / (double)processAmount;
          NSLog(@"progr: %f", progr); // Logs values between 0.0 and 1.0
          [progressbar setDoubleValue:progr];
          [progressbar needsDisplay]; // Do I need this?
  
          // Do some more hard work here...
      }
  }
  
  // This method runs when a stop button is clicked, but as long
  // as -startIt is busy, a click on the stop button does nothing.
  - (IBAction)stopIt:(id)sender {
      NSLog(@"Stop it!");
      running = NO;
      [progressbar stopAnimation:sender];
  }
  </pre>
  
  <p>I'm really new to Objective-C, Cocoa and applications with a UI. Thank you very much for any helpful answer.</p>