gpt4 book ai didi

c++ - 为什么我的 CFRunLoopTimer 没有触发?

转载 作者:可可西里 更新时间:2023-11-01 05:41:35 26 4
gpt4 key购买 nike

我在 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() 会产生以下日志输出:

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

但是,我的计时器从不触发。有什么想法吗?

最佳答案

引自docs

A timer needs to be added to a run loop mode before it will fire. To add the timer to a run loop, use CFRunLoopAddTimer. A timer can be registered to only one run loop at a time, although it can be in multiple modes within that run loop.

还要确保您的运行循环不会在计时器触发之前终止。

关于c++ - 为什么我的 CFRunLoopTimer 没有触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16340537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com