zoukankan      html  css  js  c++  java
  • 在子线程中添加定时器并且时间到后退出runloop

    //
    //  ViewController.m
    //  trefeeg
    //
    //  Created by 张凯泽 on 16/3/30.
    //  Copyright © 2016年 rytong_zkz. All rights reserved.
    //
    
    #import "ViewController.h"
    static int count = 10;
    @interface ViewController ()
    {
        BOOL end1;
        NSThread * thread;
        //NSTimer * timer;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
         thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadClick) object:nil];
        [thread start];
         //timer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
        
    }
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesEnded");
        [self performSelector:@selector(performClick) onThread:thread withObject:nil waitUntilDone:NO ];
    }
    -(void)performClick
    {
        NSLog(@"performClick");
    }
    -(void)threadClick
    {
        @autoreleasepool {
            NSLog(@"threadClick%@",[NSThread currentThread]);
            NSTimer * timer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerClick:) userInfo:nil repeats:YES];
           // NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerClick:) userInfo:nil repeats:YES];
        [timer fire];
        do {
            [[NSRunLoop currentRunLoop ]addTimer:timer forMode:NSDefaultRunLoopMode];
            [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            //[[NSRunLoop currentRunLoop]run];
        
            NSLog(@"____________%@",[NSThread currentThread]);
        }while (!end1);
            NSLog(@"%@",[NSThread currentThread]);
    
        }
            
    }
    -(void)timerClick:(NSTimer*)timer
    {
        NSLog(@"timerClick%@",[NSThread currentThread]);
        NSLog(@"count = %d",count);
        if (count==0) {
            CFRunLoopStop(CFRunLoopGetCurrent());
            //end1 = YES;
        }
            count--;
        
        
    }
    @end
  • 相关阅读:
    从关系型数据库到非关系型数据库
    2016某知名互联网公司PHP面试题及答案
    企业网站核心关键词如何去选择
    写Seo网站标题应该注意什么
    什么样的外链才是优质外链
    什么是网站物理链接结构
    需要分析竞争对手的网站哪些SEO数据
    做外链的时候应该需要注意什么
    描述标签对关键词排名有影响吗
    网站外链对排名的影响有哪些
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/5342857.html
Copyright © 2011-2022 走看看