首页 > 代码库 > IOS-GCD中的Timer

IOS-GCD中的Timer

- (IBAction)gcdResourceTestBtnPressed:(id)sender

{

    /// 初始化一个gcd队列.

    dispatch_queue_t timerQueue = dispatch_queue_create("timerQueue", 0);

    /// 创建 gcd timer.

    _timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerQueue);

    

    double interval = 2 * NSEC_PER_SEC; /// 间隔2秒

    dispatch_source_set_timer(_timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), interval, 0);

    

    /// 定时器block设置

    dispatch_source_set_event_handler(_timerSource, ^{

        NSLog(@"%@",[NSThread currentThread]);

        progressTotal += 0.1;

        dispatch_async(dispatch_get_main_queue(), ^{

            [_progressView setProgress:progressTotal];

        });

    });

    /// 唤起定时器任务.

    dispatch_resume(_timerSource);

}


2014-11-22 10:21:50.964 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}

2014-11-22 10:21:52.969 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}

2014-11-22 10:21:54.969 MultitThreadManagement[856:410093] <NSThread: 0x7fd8d3511a50>{number = 2, name = (null)}


IOS-GCD中的Timer