zoukankan      html  css  js  c++  java
  • runloop源代码

    https://github.com/zzf073/runloopDemo

    /**

     *  调度例程

     *  当将输入源安装到run loop后,调用这个协调调度例程,将源注册到客户端(可以理解为其他线程)

     *

     */

    void RunLoopSourceScheduleRoutine (void *info, CFRunLoopRef rl, CFStringRef mode)

    {

        ZXRunLoopSource* obj = (__bridge ZXRunLoopSource*)info;

    //    AppDelegate*   delegate = [[AppDelegate sharedAppDelegate];

        AppDelegate*   delegate = [[UIApplication sharedApplication]delegate];

        RunLoopContext* theContext = [[RunLoopContext alloc] initWithSource:obj andLoop:rl];

        

        //发送注册请求

        [delegate performSelectorOnMainThread:@selector(registerSource:) withObject:theContext waitUntilDone:YES];

    }

    /**

     *  处理例程

     *  在输入源被告知(signal source)时,调用这个处理例程,这儿只是简单的调用了 [obj sourceFired]方法

     *

     */

    void RunLoopSourcePerformRoutine (void *info)

    {

        ZXRunLoopSource*  obj = (__bridge ZXRunLoopSource*)info;

        [obj sourceFired];

    //    [NSTimer scheduledTimerWithTimeInterval:1.0 target:obj selector:@selector(timerAction:) userInfo:nil repeats:YES];

    }

    /**

     *  取消例程

     *  如果使用CFRunLoopSourceInvalidate/CFRunLoopRemoveSource函数把输入源从run loop里面移除的话,系统会调用这个取消例程,并且把输入源从注册的客户端(可以理解为其他线程)里面移除

     *

     */

    void RunLoopSourceCancelRoutine (void *info, CFRunLoopRef rl, CFStringRef mode)

    {

        ZXRunLoopSource* obj = (__bridge ZXRunLoopSource*)info;

        AppDelegate* delegate = [AppDelegate sharedAppDelegate];

        RunLoopContext* theContext = [[RunLoopContext alloc] initWithSource:obj andLoop:rl];

        

        [delegate performSelectorOnMainThread:@selector(removeSource:) withObject:theContext waitUntilDone:NO];

    }

    安装输入源到Run Loop---分两步首先初始化一个输入源(init),然后将这个输入源添加到当前Run Loop里面(addToCurrentRunLoop)

    CFRunLoopSourceContext  context = {0, (__bridge void *)(self), NULL, NULL, NULL, NULL, NULL,

            &RunLoopSourceScheduleRoutine,

            RunLoopSourceCancelRoutine,

            RunLoopSourcePerformRoutine};

        

        runLoopSource = CFRunLoopSourceCreate(NULL, 0, &context);

        commands = [[NSMutableArray alloc] init];

    CFRunLoopRef runLoop = CFRunLoopGetCurrent();

        CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode);

    - (void)sourceFired

    {

        NSLog(@"Source fired: do some work, dude!");

        NSThread *thread = [NSThread currentThread];

        [thread cancel];

    }

  • 相关阅读:
    浅析七种经典排序算法
    一个可编辑与新增博客园文章的 Python 脚本
    快速排序的几种实现方式
    如何查找某个网站的(如:有道云笔记)的接口
    一键导出「有道云笔记」所有笔记
    2020年启蒙及小学识字练字APP或小程序测评榜
    2020年部编版小学二年级语文上册知识点(完整版)
    2020年部编人教版小学语文一年级下册知识点汇总
    换个角度,程序员爸爸应该关注一下
    计算机基础知识-I/O篇
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8876237.html
Copyright © 2011-2022 走看看