在非主线程中运行Cocoa GUI

OCa*_*los 8 macos user-interface cocoa multithreading objective-c

我在开发cocoa用户界面时遇到了与gui/threading相关的问题.该应用程序的设计如下:

主线程(#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;
}
Run Code Online (Sandbox Code Playgroud)

Kur*_*vis 13

别.这种方法永远不会奏效.即使您修复了当前的问题(窗口没有绘制),您也会立即遇到另一个模糊的,不可能解决的问题,另一个问题,另一个问题.Cocoa期望GUI线程成为主线程,故事结束.