我在C++类中创建了一个CFRunLoopTimer
,如下所示:
#import <CoreFoundation/CoreFoundation.h>
void cClass::StartTimer()
{
if(!mActiveSenseTimer)
{
CFTimeInterval TIMER_INTERVAL = 5;
CFRunLoopTimerContext TimerContext = {0, this, NULL, NULL, NULL};
CFAbsoluteTime FireTime = CFAbsoluteTimeGetCurrent() + TIMER_INTERVAL;
mTimer = CFRunLoopTimerCreate(kCFAllocatorDefault,
FireTime,
0, 0, 0,
ActiveSenseTimerCallback,
&TimerContext);
NSLog(@"RunLoop:0x%x, TimerIsValid:%d, TimeIsNow:%f, TimerWillFireAt:%f",
CFRunLoopGetCurrent(),
CFRunLoopTimerIsValid(mActiveSenseTimer),
CFAbsoluteTimeGetCurrent(),
FireTime);
}
}
void ActiveSenseTimerCallback(CFRunLoopTimerRef timer, void *info)
{
NSLog(@"Timeout");
CFRunLoopTimerContext TimerContext;
TimerContext.version = 0;
CFRunLoopTimerGetContext(timer, &TimerContext);
((cClass *)TimerContext.info)->Timeout();
}
调用
cClass::StartTimer()
会导致以下日志输出:但是,我的计时器永远不会触发。有什么想法吗?
最佳答案
引用docs
另外,请确保您的运行循环不会在计时器触发之前消失。