Problem in Background Thread in iPhone
I am using Background Thread to update one of my label
I am using the following code. But in iOS 4.0 i have learn that application saves its states and goes to background. and my application also did that work but the thread i am using stops working when i hide the application and again resumes from where i left when i reopen it. Can anybody please tell me what do I need to change in code in order to make the thread keep on running in background and change my GUI while my application is hidden. I am using this code..
-(void)startThread
{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(setUpTimerThrea开发者_如何学God) object:nil];
[thread start];
}
-(void)setUpTimerThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTimer *timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(triggerTimer) userInfo:nil repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
[runLoop run];
[pool release];
}
-(void)triggerTimer
{
NSLog(@"***Timer Called after 3 seconds*** %d",count);
self.label.text = [NSString stringWithFormat:@"count value = %d",count];
//self.titleLabel.text= [NSString stringWithFormat:@"count value = %d",count];
count = count +1;
}
Thanks
Timers won't work on relaunch of the application. What you need to do is reinitialize the timer from your appDelegate's applicationDidBecomeActive: method and make sure you shut down the timer from applicationWillEnterBackground: method
精彩评论