且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

回调方法到Apple运行循环

更新时间:2023-12-03 18:59:22

文档没有列出 IOPowerSourceCallbackType 类型,但它在< IOKit / ps / IOPowerSources.h> 标头为:

The documentation doesn't list the IOPowerSourceCallbackType type, but it's declared in the <IOKit/ps/IOPowerSources.h> header as:

typedef void  (*IOPowerSourceCallbackType)(void *context);

这意味着您将回调定义为:

That means you would define your callback as:

void callback(void *context)
{
    // ...
}

您可以使用以下代码将其传递给 IOPSNotificationCreateRunLoopSource

You would pass that in to IOPSNotificationCreateRunLoopSource using code like:

CFRunLoopSourceRef rls = IOPSNotificationCreateRunLoopSource(callback, whateverValueIsMeaningfulToYourCallback);
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);

您想仔细考虑要在哪个模式下安排源的运行循环。如果你需要在以后的时间对运行循环源( rls )做更多的事情,那么不要立即释放它。保持在一个实例变量或类似的东西,释放它,当你完成它。特别是,你可能想要使用 CFRunLoopSourceInvalidate()在某一时刻使它无效。

You want to carefully consider which run loop you want to schedule the source on and in which mode. If you need to do further stuff to the run loop source (rls) at later times, then don't release it immediately. Keep it in an instance variable or something like that and release it when you're done with it. In particular, you may want to invalidate it using CFRunLoopSourceInvalidate() at some point.