2012-09-10 4 views
1

다음은 내 프로그램의 일부입니다. findDuplicates의 루프는 버튼을 누른 후 백그라운드 스레드에서 시작됩니다. 다른 버튼을 눌러 스레드/루프를 중지/종료 할 수있는 방법이 있습니까? 스레드을 종료 에서루프/스레드 종료 방법

Threading Programming Guide에서 읽기
- (IBAction)countDups:(id)sender { 
    [self performSelectorInBackground:@selector(findDuplicates) withObject:nil]; 
} 

-(void)findDuplicates 
{ 
... 
for(int index=0;index<self.resultList.count;index++) 
{ ... } 
... 
} 

답변

2

배경 스레드에서 복귀 할 수 있습니다. 하나의 멤버 변수를 만들고, 그것을 NO로 초기화하십시오.

- (IBAction)countDups:(id)sender { 
    mCancel = NO; 
    [self performSelectorInBackground:@selector(findDuplicates) withObject:nil]; 
} 

-(IBAction)stop 
{ 
    mCancel = YES; //BOOL member variable; 
} 

-(void)findDuplicates 
{ 
... 
for(int index=0;index<self.resultList.count;index++) 
{ 
    If(mCancel) 
    return; // return for thread to end 

    ... } 
... 
} 
0

. performSelectorInBackground:withObject:은 기본적으로 새 스레드를 만들고 그 스레드에서 코드를 실행합니다.