问题
I have an application for which the UI element includes an NSStatusItem
and a menu. Inside my application , I am using NSTask
asynchronously to perform some operation and I am using the output obtained using the NSFileHandleReadCompletionNotification
to update the menu. But now whenever I click and open the menu , the main runloop goes into NSEventTrackingRunLoopMode
and the notification posting fails. So basically with my menu open , no operation takes place on the main thread. Now I found a similar problem on this post but the accepted solution there does not seem to help. I understand that for NSNotificationCenter
, the NSRunloopDefaultMode
must not be blocked. Is there anyway to alter this bahaviour??? Can anyone please help ???
回答1:
What I'm guessing is that you have an NSFileHandle
that represents the NSTask
's stdout
, and you've asked it to -readInBackgroundAndNotify.
The problem with this, as you've found, is that this only notifies when the runloop is in the default mode. If the runloop enters another mode (such as when a menu is open), then your notifications will queue up on the main runloop and will wait until the runloop re-enters the default mode.
What you want to use instead is the -readInBackgroundAndNotifyForModes: method, to which you would pass an array containing both NSDefaultRunLoopMode
and NSEventTrackingRunLoopMode
. This would indicate to the runloop that you want to be notified of any available data when the runloop is either in the default mode or the event tracking mode.
来源:https://stackoverflow.com/questions/10881122/main-thread-runloop-gets-blocked-on-opening-nsmenu