【发布时间】:2012-04-03 10:52:48
【问题描述】:
我在开发可可用户界面时遇到了与 gui/线程相关的问题。应用程序是这样设计的:
主线程 (#1):解析参数、加载插件等
Gui 线程 (#?):启动 gui,处理事件等。它是 gui 线程。
Cocoa 框架是非线程安全的,但强制执行一条规则,GUI 必须在主线程上运行。断言用于检查这一点。为了解决这个问题,我自己实现了 run 方法(下面的代码),遵循这个 - http://cocoawithlove.com/2009/01/demystifying-nsapplication-by.html - 指南。但我错过了一些东西。一个窗口打开,但保持空白(完全白色)。虽然如果我在主线程中进行调用,它会完美运行。
所以基本上我需要弄清楚缺少什么。
- (void)run
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self finishLaunching];
shouldKeepRunning = YES;
do
{
[pool release];
pool = [[NSAutoreleasePool alloc] init];
NSEvent *event =
[self
nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantFuture]
inMode:NSDefaultRunLoopMode
dequeue:YES];
[self sendEvent:event];
[self updateWindows];
} while (shouldKeepRunning);
[pool release];
}
- (void)terminate:(id)sender
{
shouldKeepRunning = NO;
}
【问题讨论】:
-
看起来你应该只反转你使用线程的东西。为什么要对抗框架?
-
因为我宁愿不与程序的插件架构作斗争...但我想我将不得不承认这一点...
标签: objective-c multithreading macos cocoa user-interface