(转)在新线程中使用NSTimer
2012-07-31 16:18
309 查看
(转)在新线程中使用NSTimer
转自:NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
转自:NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
方法一: -(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
- NSTimer直接使用需要在主线程中使用
- NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
- 在新线程中使用NSTimer
- NStimer 在滚动ScrollView的时候停止,在新线程中使用NSTimer
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- 单独线程上使用RunLoop开启NSTimer
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
- NSTimer和NSRunLoop在子线程中的使用
- 在新线程中使用NSTimer
- NSTimer和NSRunLoop在子线程中的使用
- iOS 关于dispatch_semaphore_t(信号量) 和 dispatch_group_t (组) 线程执行顺序的使用
- java线程中锁存器CountDownLatch的使用
- 线程的几种状态及相关方法使用
- java 线程中 stop()和 suspend()方法为何不推荐使用?
- android 中native层使用线程实现定时器功能
- 线程的概念?使用线程的好处?及线程与进程的区别?
- 使用MDC为Logback slf4 日志记录线程ID,区分每次执行的会话日志
- 线程的使用