iPhone背景线程中的问题 [英] Problem in Background Thread in iPhone

查看:113
本文介绍了iPhone背景线程中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用后台线程来更新我的一个标签

I am using Background Thread to update one of my label

我正在使用以下代码。但是在iOS 4.0中,我已经了解到应用程序可以保存其状态并转到后台。我的应用程序也做了那项工作,但我正在使用的线程停止工作时,我隐藏应用程序,并再次从我重新打开时离开的地方恢复。任何人都可以告诉我,我需要更改代码,以便让线程继续在后台运行,并在我的应用程序被隐藏时更改我的GUI。我正在使用此代码..

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(setUpTimerThread) 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;
}

谢谢

推荐答案

定时器无法重新启动应用程序。您需要做的是从appDelegate的applicationDidBecomeActive:方法重新初始化计时器,并确保从applicationWillEnterBackground关闭计时器:方法

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

这篇关于iPhone背景线程中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆