博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CFRunLoop 学习 例子,控制 runloop 时间
阅读量:2347 次
发布时间:2019-05-10

本文共 2581 字,大约阅读时间需要 8 分钟。

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //这里偷个懒,直接使用performSelectorInBackground来创建一个线程,并执行configRunLoop方法    [self performSelectorInBackground:@selector(configRunLoop) withObject:nil];        UIButton* __button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    [__button1 setTitle:@"Fire Event" forState:UIControlStateNormal];    //触发事件启动RunLoop    [__button1 addTarget:self action:@selector(triggerEvent) forControlEvents:UIControlEventTouchUpInside];    __button1.frame = CGRectMake(20, 100, 100, 80);    [self.view addSubview:__button1];            UIButton* __button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    [__button2 setTitle:@"Stop RunLoop" forState:UIControlStateNormal];    //RunLoop周期完成后自动退出线程    [__button2 addTarget:self action:@selector(stopRunloop) forControlEvents:UIControlEventTouchUpInside];    __button2.frame = CGRectMake(130, 100, 120, 80);    [self.view addSubview:__button2];}- (void)stopRunloop{    _shouldStop = YES;}- (void)triggerEvent{    if (CFRunLoopIsWaiting(_runLoopRef)) {        NSLog(@"RunLoop 正在等待事件输入");        //添加输入事件        CFRunLoopSourceSignal(_source);        //唤醒线程,线程唤醒后发现由事件需要处理,于是立即处理事件        CFRunLoopWakeUp(_runLoopRef);    }else {        NSLog(@"RunLoop 正在处理事件");        //添加输入事件,当前正在处理一个事件,当前事件处理完成后,立即处理当前新输入的事件        CFRunLoopSourceSignal(_source);    }}//此输入源需要处理的后台事件static void fire(void* info __unused){    NSLog(@"我现在正在处理后台任务");    sleep(5);}- (void)configRunLoop{    //这里获取到的已经是某个子线程了哦,不是主线程哦    _tThread = [NSThread currentThread];    //这里也是这个子线程的RunLoop哦    _runLoopRef = CFRunLoopGetCurrent();        bzero(&_source_context, sizeof(_source_context));    //这里创建了一个基于事件的源    _source_context.perform = fire;    _source = CFRunLoopSourceCreate(NULL, 0, &_source_context);    //将源添加到当前RunLoop中去    CFRunLoopAddSource(_runLoopRef, _source, kCFRunLoopCommonModes);            while (!_shouldStop) {        NSLog(@"RunLoop 开始运行");        //每次RunLoop只运行10秒,每10秒做一次检测,如果没有需要处理的后台任务了,就让此线程自己终止,不用暴力Kill        CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, NO);        NSLog(@"RunLoop 停止运行");    }    _tThread = nil;}@end

 

    CFRunLoopSourceRef    _source;

    CFRunLoopSourceContext    _source_context;
    NSThread*              _tThread;
    CFRunLoopRef     _runLoopRef;

 

@interface ViewController : UIViewController{    CFRunLoopSourceRef _source;    CFRunLoopSourceContext _source_context;    NSThread* _tThread;    CFRunLoopRef _runLoopRef;    BOOL _shouldStop;}@end

 

转载地址:http://tlxvb.baihongyu.com/

你可能感兴趣的文章
系统--A disk read error occurred Press Ctrl+Alt+d...
查看>>
Some projects cannot be imported because they a...
查看>>
ubuntu-android--make: *** [out/host/linux-x86/o...
查看>>
原子变量与synchronized详细解释
查看>>
java.lang.OutOfMemoryError: PermGen space及其解决方法
查看>>
如何让ajaxfileupload.js支持IE9,IE10,并可以传递多个参数?
查看>>
highcharts扩展tooltip提示异步信息
查看>>
activiti--History 历史配置
查看>>
activiti--部署bpmn/bar文件详解
查看>>
win7使用Putty 连接debain
查看>>
debain 常用命令
查看>>
debain 安装amd显卡驱动
查看>>
Java Jacob 打印word文档
查看>>
Java Freemarker 根据模板生成Word
查看>>
Java Mybatis Plus 集成与使用
查看>>
Java 一台电脑部署多个tomcat服务
查看>>
Java WinSw 安装Jar成Windows服务
查看>>
Linux安装Jar成服务
查看>>
Java SSH连接mysql数据库
查看>>
计算机使用常见问题与答案
查看>>