给CFRunLoop添加Timer

最近在学习NSThread的应用,既然学习了NSThread就免不了学习CFRunLoop。今天便学习了给Loop添加timer的方法。直接贴代码。

#import <CoreFoundation/CoreFoundation.h>

//#import "AppDelegate.h"


static void _timer(CFRunLoopTimerRef timer __unused, void *info)
{
   // CFRunLoopSourceSignal(info);
    (*(int *)(info))++;
    NSLog(@"%d",*(int *)info);
}


int main(int argc, char *argv[])
{
    @autoreleasepool {
        int i=0;
        CFRunLoopTimerRef timer;
        CFRunLoopTimerContext timer_context;
        
        bzero(&timer_context, sizeof(timer_context));
        timer_context.info = &i;
        timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 1, 0, 0, _timer, &timer_context);
        CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
        CFRunLoopRun();
        
       // returnUIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegateclass]));
    }
}

  最后的结果自然是:

2012-11-22 23:09:51.568 runloop[32118:707] 1

2012-11-22 23:09:52.568 runloop[32118:707] 2

2012-11-22 23:09:53.569 runloop[32118:707] 3

2012-11-22 23:09:54.569 runloop[32118:707] 4

2012-11-22 23:09:55.568 runloop[32118:707] 5

2012-11-22 23:09:56.569 runloop[32118:707] 6

2012-11-22 23:09:57.569 runloop[32118:707] 7

2012-11-22 23:09:58.568 runloop[32118:707] 8

2012-11-22 23:09:59.569 runloop[32118:707] 9

2012-11-22 23:10:00.569 runloop[32118:707] 10

变量i的值随这timer的增加二增加~~~希望对大家有所帮助!!!!

转载于:https://www.cnblogs.com/GrowUP-EveryDay/archive/2012/11/22/2783645.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值