小编OCa*_*los的帖子

在非主线程中运行Cocoa GUI

我在开发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)

macos user-interface cocoa multithreading objective-c

8
推荐指数
1
解决办法
3352
查看次数

默认音频输出 - 获取设备更改通知?(CoreAudio,Mac OS X,AudioHardwareAddPropertyListener)

我正在尝试使用CoreAudio API编写一个监听器,用于更改默认音频输出(例如:插入耳机插孔).我找到了示例代码,虽然有点旧并且使用了已弃用的函数(http://developer.apple.com/mac/library/samplecode/AudioDeviceNotify/Introduction/Intro.html,但它没有用.重新编写代码使用AudioHardwareAddPropertyListener方法以'正确'的方式,但它似乎仍无法正常工作.当我插入耳机时,我注册的功能没有被触发.我在这里有点亏...我怀疑问题可能在其他地方,但我无法弄清楚在哪里......

监听器注册码:

OSStatus err = noErr;
AudioObjectPropertyAddress audioDevicesAddress = { kAudioHardwarePropertyDefaultOutputDevice, KAudioObjectPropertyScopeGlobal, KAudioObjectPropertyElementMaster };
err = AudioObjectAddPropertyListener ( KAudioObjectAudioSystemObject, &AudioDevicesAddress, coreaudio_property_listener, NULL);
if (err) trace ("error on AudioObjectAddPropertyListener");
Run Code Online (Sandbox Code Playgroud)

audio core-audio listener

3
推荐指数
1
解决办法
2714
查看次数