zoukankan      html  css  js  c++  java
  • OC中几种延时操作的比較

    1. sleepForTimeInterval,此函数会卡住当前线程,一般不用

    <span style="font-size:18px;">[NSThread sleepForTimeInterval:3];</span>


    2. performSelector,定制好延迟任务后,不会卡主"当前线程"(3秒后运行download:方法)

    <span style="font-size:18px;">[self performSelector:@selector(download:) withObject:@"http://555.jpg" afterDelay:3];</span>


    3.3秒后回到"主线程"运行block中的代码

    <span style="font-size:18px;">dispatch_queue_t queue = dispatch_get_main_queue();</span>
    <span style="font-size:18px;"><span style="font-size:18px;">dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"------task------%@", [NSThread currentThread]);
    });    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"------task------%@", [NSThread currentThread]);
    })</span>


    4.3秒后自己主动开启"新线程"运行block中的代码

    <span style="font-size:18px;">dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);</span>
    <span style="font-size:18px;"><span style="font-size:18px;">dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{
        NSLog(@"------task------%@", [NSThread currentThread]);
    });</span>

  • 相关阅读:
    如何让WPF程序用上MVVM模式
    wpf开源界面收集
    WPF界面框架的设计
    WPF数据验证
    WPF实用知识点
    wpf的MVVM框架
    数据库中树形结构的表的设计
    ASP.NET MVC 分部视图
    好用的 Visual Studio插件
    ASP.NET MVC3中Controller与View之间的数据传递总结
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/7384544.html
Copyright © 2011-2022 走看看