Why is my CFRunLoopTimer not shooting?

I have a CFRunLoopTimer created in a C ++ class as shown below:

 #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(); } 

Calling cClass::StartTimer() results in the following log output:

RunLoop: 0x7655d60, TimerIsValid: 1, TimeIsNow: 389196910.537962, TimerWillFireAt: 389196915.537956

However, my timer never fires. Any ideas why?

+4
source share
1 answer

Quote from docs

The timer must be added to the run cycle mode before it starts. To add a timer to the run loop, use CFRunLoopAddTimer . A timer can only be registered for one run cycle at a time, although it can be in several modes in this run cycle.

Also make sure that the run loop does not die before the timer fires.

+4
source

All Articles