来自 C++ 的 NSOpenPanel增强线程
我从 boost C++ 创建的线程调用 NSOpenPanel。
面板行为不稳定并且对鼠标的响应不佳,也就是说,当单击顶级组合框确实提高了响应时,单击对象有时不会执行任何操作。
我必须运行一个单独的运行循环吗?我正在执行一个 runModalForDirectory ,它应该负责运行自己的循环。
我还创建了一个单独的 objc 类,它执行 PerformSelectorOnMainThread 以在主线程中显示面板,但行为仍然相同。
[ps performSelectorOnMainThread:@selector(showOpenPanel) withObject:nil
waitUntilDone:YES
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
我还尝试过 waitUntilDone:NO 并运行 CFRunLoopRunInMode 这也没有帮助。
- (bool) showOpenPanel
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:YES];
[op setTitle:@"Choose File"];
[op setMessage:@"Choose file for Importing."];
[op setFloatingPanel:true];
bool result =[op runModalForDirectory:NSHomeDirectory() file:nil types:self.fileTypes];
if (result == NSOKButton) {
[self setSelectedFiles:[op filenames]];
[self setLastShowResult:true];
}
else {
[self setLastShowResult:false];
}
[self setPanelIsDone:true];
return self.lastShowResult;
}
I'm invoking a NSOpenPanel from a thread created by boost C++.
the panel behaves erratically and doesn't respond well to mouse, that is clicking on objects does nothing sometime when clicking on top level combo box does improve response.
do i've to run a separate runloop I'm doing a runModalForDirectory which should take care of running its own loop.
I've also created a separate objc class which does performSelectorOnMainThread to show panel in main thread but still the behavior is same.
[ps performSelectorOnMainThread:@selector(showOpenPanel) withObject:nil
waitUntilDone:YES
modes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
I've also tried with waitUntilDone:NO and running a CFRunLoopRunInMode which isn't helping either.
- (bool) showOpenPanel
{
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setAllowsMultipleSelection:YES];
[op setTitle:@"Choose File"];
[op setMessage:@"Choose file for Importing."];
[op setFloatingPanel:true];
bool result =[op runModalForDirectory:NSHomeDirectory() file:nil types:self.fileTypes];
if (result == NSOKButton) {
[self setSelectedFiles:[op filenames]];
[self setLastShowResult:true];
}
else {
[self setLastShowResult:false];
}
[self setPanelIsDone:true];
return self.lastShowResult;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSOpenPanel
是 AppKit 的一部分。 AppKit 函数和类只能在主线程上安全使用。向我们展示您在
performSelectorOnMainThread
中使用的代码,以便我们帮助找出您可能仍然遇到问题的原因。我怀疑你正在用它调用单独的方法——不要这样做;它不会按照你期望的方式工作。回调主线程以获取与NSOpenPanel
的全部交互。NSOpenPanel
is part of AppKit. AppKit functions and classes can only be safely used on the main thread.Show us the code you used with
performSelectorOnMainThread
so we can help figure out why you might still be seeing problems. I suspect you're calling individual methods with it--don't; it won't work the way you expect. Call back to the main thread for the totality of your interaction withNSOpenPanel
.