[转帖]IOS在子线程中使用定时器,将定时器添加至RunLoop中_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3477 | 回复: 0   主题: [转帖]IOS在子线程中使用定时器,将定时器添加至RunLoop中        下一篇 
baijiang.lu
注册用户
等级:少校
经验:947
发帖:81
精华:0
注册:2013-9-2
状态:离线
发送短消息息给baijiang.lu 加好友    发送短消息息给baijiang.lu 发消息
发表于: IP:您无权察看 2013-9-6 16:06:47 | [全部帖] [楼主帖] 楼主

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

02{

    03self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    04

    05//用NSObject的方法创建一个多线程

    06[self performSelectorInBackground:@selector(multiThread) withObject:nil];

    07

    08self.window.backgroundColor = [UIColor whiteColor];

    09[self.window makeKeyAndVisible];

    10returnYES;

11}

12- (void)multiThread

13{

    14NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    15if(![NSThread isMainThread]) {

    16

    17// 第1种方式

    18//此种方式创建的timer已经添加至runloop中

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

    20//保持线程为活动状态,才能保证定时器执行

    21//        [[NSRunLoop currentRunLoop] run];//已经将nstimer添加到NSRunloop中了

    22

    23//第2种方式

    24//此种方式创建的timer没有添加至runloop中

    25NSTimer *timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];

    26//将定时器添加到runloop中

    27[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

    28[[NSRunLoop currentRunLoop] run];

    29NSLog(@"多线程结束");

    30}

    31[pool release];

32}

33

34- (void)timerAction

35{

    36//定时器也是在子线程中执行的

    37if(![NSThread isMainThread]) {

    38NSLog(@"定时器");

    39}

40}




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论