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
  • 相关阅读:
    20145206 《信息安全系统设计基础》第3周学习总结
    20145206 《信息安全系统设计基础》第2周学习总结
    Alpha 冲刺 (3/10)
    Alpha 冲刺 (2/10)
    Alpha 冲刺 (1/10)
    福大软工 · 第七次作业
    福大软工 · 第八次作业(课堂实战)- 项目UML设计(团队)
    福大软工1816 · 第六次作业
    福大软工1816 · 第五次作业
    福大软工1816 · 第四次作业
  • 原文地址:https://www.cnblogs.com/zkzzkz/p/5342857.html
Copyright © 2011-2022 走看看