Main Thread Runloop gets blocked on opening nsmenu

筅森魡賤 提交于 2019-12-31 01:41:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!