title: Display contextual NSMenu without blocking main thread
body: <p>I had some unexpected results when displaying a contextual <code>NSMenu</code>. I'm presenting the menu myself, not using the <code>menu</code> property of an <code>NSResponder</code>. It turned out, presenting an <code>NSMenu</code> is a blocking operation.</p>
  
  <p>In a new template project I only added the following code in <code>applicationDidLaunch:</code></p>
  
  <pre><code>NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Menu"];
  [menu addItemWithTitle:@"Item 1" action:@selector(selected:) keyEquivalent:@""];
  [menu addItemWithTitle:@"Item 2" action:@selector(selected:) keyEquivalent:@""];
  [menu addItemWithTitle:@"Item 3" action:@selector(selected:) keyEquivalent:@""];
  [menu popUpMenuPositioningItem:[menu itemAtIndex:0] 
                      atLocation:CGPointMake(500, 500) 
                          inView:nil];
  NSLog(@"Context menu shown.");
  </code></pre>
  
  <p>The <code>popUpMenuPositioningItem:atLocation:inView:</code> call doesn't return until I select an item or click outside to automatically dismiss the menu. The log is only printed once the menu is closed.</p>
  
  <p>Why does this happen? And how can I prevent it? I found a couple of mentions of this problem, but the "solutions" always consisted of executing the other things in the background, instead of preventing <code>NSMenu</code> from blocking. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>If I present the menu in a background thread, the menu becomes unresponsive, doesn't react to clicks inside or outside.</p>
  
  <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
      [[menu popUpMenuPositioningItem:[menu itemAtIndex:0] 
                           atLocation:CGPointMake(500, 500) 
                               inView:nil];
  });
  </code></pre>