2011-08-25 81 views
0

我正在使用後臺線程來更新我的標籤之一在iPhone中的背景線程問題

我正在使用以下代碼。但在iOS 4.0中,我已經瞭解到應用程序會保存其狀態並轉到後臺。我的應用程序也做了這項工作,但我使用的線程停止工作,當我隱藏應用程序,並再次從我離開時,我重新打開時恢復。任何人都可以告訴我,爲了讓線程繼續在後臺運行並在隱藏應用程序時更改我的GUI,需要在代碼中更改哪些內容。我使用這個代碼..

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

感謝

回答

1

定時器不會對應用程序的重新推出工作。你需要做的是從你的appDelegate的applicationDidBecomeActive:方法重新初始化定時器,並確保你關閉了從applicationWillEnterBackground定時器:方法

+0

不,你弄錯了我。我需要在後臺運行線程....我不想讓我的線程停止.... – Shah

+0

你的線程不能在後臺運行,除非它落入特定的類別。例如VOIP應用程序可以在後臺執行。 你可以在這裏找到更多的信息:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html – Ved

+1

是的,但我已經解決了,如果我把這個線程委託類然後在iOS 4.0中不會停止線程。並繼續做任何分配給它的東西。 :)感謝Ved For ur Help :) – Shah