StatusItem 菜单在打开时会阻塞主线程。解决方法?
我为 Mac 编写了一个应用程序,该应用程序被设计为状态栏项目。但是,当用户从状态栏打开菜单时,主运行循环将被阻止,直到其关闭。由于此应用程序响应套接字上的消息,因此存在一个问题,即在菜单打开时它无法执行任何操作。
我尝试从单独的线程设置状态项并在不同的线程上调度套接字,但没有骰子。有没有好的办法来处理这个问题呢?
更新:
我现在已经解决了这个问题。我正在使用 NetSocket 套接字包装器,它的异步特性使其非常困难在不同的线程上打开并观看。我切换到 SmallSockets (另一个 Objective-C 套接字包装器),因为它是同步的,所以我能够打开一个套接字并直接在单独的线程上观看它。
I've written an app for the Mac that is designed as a status bar item. However, when a user opens its menu from the status bar, the main run loop is blocked until it's closed. Since this app responds to messages on a socket, it's a problem that it can't do anything while the menu is open.
I've tried setting the status item from a separate thread and scheduling the socket on a different thread, but no dice. Is there a good way to deal with this?
UPDATE:
I've resolved this now. I was using the NetSocket socket wrapper and its asynchronous nature made it very difficult to open and watch on a different thread. I switched to SmallSockets (another Objective-C socket wrapper) and because it is synchronous, I was able to open a socket and just watch it directly on a separate thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当用户与菜单交互时,运行循环在事件跟踪模式下运行。将您的套接字也附加到 NSEventTrackingRunLoopMode 模式,并且它们在用户与菜单交互时继续运行。
但是将套接字放在另一个线程上也应该可以。如果这对你不起作用,你可能做错了什么,但没有看到代码我不能说。
While the user is interacting with a menu the run loop runs in the event tracking mode. Attach your sockets to the
NSEventTrackingRunLoopMode
mode too and they continue to run while the user interacts with the menu.But putting the sockets on another thread should work too. If this did not work for you, you probably did something wrong, but without seeing the code I can't say.