我在開發可可用戶界面時遇到了與gui /線程相關的問題。該應用程序是這樣設計的:在非主線程中運行可可GUI
主線程(#1):解析參數加載插件等
GUI線程(#):啓動GUI,處理事件等,其GUI線程。
Cocoa框架是非線程安全的,但強制執行一條規則,GUI必須在主線程上運行。斷言用於檢查這一點。爲了嘗試去解決這個問題,我自己實現了這個運行方法(代碼如下) - http://cocoawithlove.com/2009/01/demystifying-nsapplication-by.html - guide。但我錯過了一些東西。窗戶打開,但保持空白(完全白色)。雖然如果我在主線程中進行調用,它可以很好地工作。
所以基本上我需要找出缺少的東西。
- (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;
}
看起來你應該扭轉你正在使用你的線程的東西。爲什麼要打架? – 2012-03-18 20:48:26
因爲我寧願不打架該程序的插件架構...但我想我必須承認這一個... – OCarlos 2012-03-18 22:46:41