简体   繁体   English

在C ++程序中从NSNotificationServer获取通知

[英]Get notification's from NSNotificationServer in C++ program

I'm creating a simple C++ application with cmake on Mac. 我正在Mac上用cmake创建一个简单的C ++应用程序。 There is a main in C++ source code file, that create C++ class. C ++源代码文件中有一个主要文件,用于创建C ++类。 Inside this class I'm allocating objective C object, that adding itself to observers in NSNotificationCenter. 在此类中,我正在分配目标C对象,该对象将自身添加到NSNotificationCenter中的观察者中。 And I'm not receiving those notifications. 而且我没有收到这些通知。 There is a code: 有一个代码:

Notifications.h Notifications.h

class LaunchNotification {
public:
    LaunchNotification();
    virtual ~LaunchNotification();

    void StartNotifications();
    void StopNotifications();

private:
    void    *monitor;
};

Notifications.mm Notifications.mm

@interface Monitor : NSObject
-(id) init;
-(void) appLaunchedNotification :(NSNotification *) notification;
-(void) appTerminatedNotification :(NSNotification *) notification;
@end

@implementation Monitor

- (id) init
{
    self = [super init];
    if (self)
    {
        count = 0;
        NSNotificationCenter *notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

        [notCenter addObserver : self
            selector:@selector(appLaunchedNotification:)
            name:NSWorkspaceDidLaunchApplicationNotification
            object:nil];

        [notCenter addObserver : self
            selector:@selector(appTerminatedNotification:)
            name:NSWorkspaceDidTerminateApplicationNotification
            object:nil];
    }

    return self;
}

- (void) appLaunchedNotification : (NSNotification *) notification
{
    NSString *path = [[notification userInfo]objectForKey: @"NSApplicationPath"];
}

- (void) appTerminatedNotification : (NSNotification *) notification
{
    NSString *path = [[notification userInfo]objectForKey: @"NSApplicationPath"];
}

- (void) dealloc
{
    NSNotificationCenter *notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

    [notCenter removeObserver : self];

    [super dealloc];
}
@end

LaunchNotification::LaunchNotification() : monitor(NULL)
{}

LaunchNotification::~LaunchNotification()
{
    StopNotifications();
}

void LaunchNotification::StartNotifications()
{
    if (NULL == monitor)
    {
        monitor = [[Monitor alloc] init];
    }
}

void LaunchNotification::StopNotifications()
{
    if (NULL != monitor)
    {
        [(id)monitor release];
    }
}

You need a run loop because otherwise, NSWorkspace has no mechanism to gain control of your application's thread in order to post notifications. 您需要一个运行循环,因为否则,NSWorkspace没有机制来获得对应用程序线程的控制以发布通知。

While the docs say run loops are automatically created, they are not automatically executed. 虽然文档说运行循环是自动创建的,但不会自动执行。 Think about it: how can a thread be simultaneously running your code and running the code in the run loop? 考虑一下:线程如何同时运行您的代码和在运行循环中运行代码?

Any tasks that you need to do while you are monitoring the notification centre need to be done in the context of runloop events eg on an NSTimer event, or you need a separate thread for that other stuff. 您在监视通知中心时需要执行的所有任务都需要在runloop事件的上下文中完成,例如在NSTimer事件上,或者需要用于其他内容的单独线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM