在新线程中使用NSTimer
2011-11-08 12:57
411 查看
方法一:
-(void) viewDidLoad{
[self performSelectorInBackground:@selector(call1) withObject:nil];
}
-(void) call1{
timer1 = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSRunLoopCommonModes];
}
-(void) call2{
// do something
timer1 invalidate];
timer1 = nil;
}
方法二:
-(void) viewDidLoad{
[self performSelectorInBackground:@selector(call1) withObject:nil];
}
-(void) call1{
timer1 = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(doSomething) userInfo:nil repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSRunLoopCommonModes];
}
-(void) call2{
// do something
timer1 invalidate];
timer1 = nil;
}
方法二:
- (void)viewDidAppear:(BOOL)animated { NSThread* timerThread = [[NSThread alloc] initWithTarget:self selector:@selector(timerStart) object:nil]; [timerThread start]; } -(void)timerStart { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; timer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(method) userInfo:nil repeats:YES] retain];//一定要retain,不然timerStart执行完后,NSTimer就被释放了。 [runLoop run]; [pool release]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [timer invalidate]; }
方法三:
timer = [NSTimer timerWithTimeInterval:5.0 target:self selector:@selector(SendHeartBeat) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:heartTimer forMode:NSDefaultRunLoopMode];
相关文章推荐
- NSTimer和NSRunLoop在子线程中的使用
- (转)在新线程中使用NSTimer
- 在新线程中使用NSTimer
- NSTimer和NSRunLoop在子线程中的使用
- NSTimer直接使用需要在主线程中使用
- NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
- 在新线程中使用NSTimer
- NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- 单独线程上使用RunLoop开启NSTimer
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- mysql 需要内核级线程的支持,而不只是用户级线程,这样才能够有效的使用多个cpu
- FIFO和线程的使用
- MFC线程使用
- VC++ .NET UI同步(使用Invoke托管方法),异步线程调用UI
- 线程间通信方式(wait和notify的使用)
- 【iOS沉思录】如何创建NSTimer并使其在当前线程中正常运行?
- 使用线程新建WPF窗体(公用进度条窗体)