我使用 NSTimer 和 NSEventTrackingRunLoopMode 来解决类似的问题。
在您的主线程中,创建一个计时器:
updateTimer = [[NSTimer scheduledTimerWithTimeInterval:kSecondsPerFrame target:self selector:@selector(update:) userInfo:nil repeats:YES] retain];
// kpk important: this allows UI to draw while dragging the mouse.
// Adding NSModalPanelRunLoopMode is too risky.
[[NSRunLoop mainRunLoop] addTimer:updateTimer forMode:NSEventTrackingRunLoopMode];
然后在你的更新:例程中,检查 NSEventTrackingRunLoopMode:
// only allow status updates and drawing (NO show data changes) if App is in
// a Modal loop, such as NSEventTrackingRunLoopMode
NSString *runLoopMode = [[NSRunLoop currentRunLoop] currentMode];
if ( runLoopMode == NSEventTrackingRunLoopMode )
{
... do periodic update tasks that are safe to do while
... in this mode
... I *think* NSMenu is just a window
for ( NSWindow *win in [NSApp windows] )
{
[win displayIfNeeded];
}
return;
}
// do all other updates
...