title: How to correctly display a "progress" sheet modally while using Grand Central Dispatch to process something?
body: <p>I'm trying to display a sheet on a window containing a single progress bar, to show the progress of some long function running asynchronously using Grand Central Dispatch.  I've almost got it, but can't get the sheet to appear to be in focus, probably because I haven't used <code>runModalForWindow:</code> or similar.</p>
  
  <p>This is approximately what I'm doing at the moment, it happens as a result of a button press on the main window:</p>
  
  <pre><code>    // Prepare sheet and show it...
  
      [NSApp beginSheet:progressSheet modalForWindow:window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
  
      [progressSheet makeKeyAndOrderFront:self];
  
      [progressBar setIndeterminate:NO];
      [progressBar setDoubleValue:0.f];
      [progressBar startAnimation:self];
  
  
      // Start computation using GCD...
  
      dispatch_async(dispatch_get_global_queue(0, 0), ^{
  
          for (int i = 0; i &lt; 1000; i ++) {
              // Do some large computation here
              // ...
  
              // Update the progress bar which is in the sheet:
              dispatch_async(dispatch_get_main_queue(), ^{
                  [progressBar setDoubleValue:(double)i];
              });
          }
  
  
          // Calculation finished, remove sheet on main thread
  
          dispatch_async(dispatch_get_main_queue(), ^{
              [progressBar setIndeterminate:YES];
  
              [NSApp endSheet:progressSheet];
              [progressSheet orderOut:self];
          });
      });
  </code></pre>
  
  <p>This works, except the main window is still in focus, the sheet is out of focus, and the progress bar doesn't animate (unless I use <code>setUsesThreadedAnimation:YES</code> on it).</p>
  
  <p>The problem I think I'm having is that I'm not sure how to run the sheet modally without blocking the main thread before I start the asynchronous computation?</p>