2012-09-10 4 views
1

Voici une partie de mon programme. Loop in findDuplicates démarre dans le fil d'arrière-plan après avoir appuyé sur le bouton. Y at-il un moyen d'arrêter/tuer thread/boucle en appuyant sur un autre bouton?Comment faire pour terminer la boucle/thread

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

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

Répondre

2

Vous pouvez revenir du fil d'arrière-plan. créer une variable membre, l'initialiser avec 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

Lire dans le Threading Programming Guide sous un fil Mettre fin. performSelectorInBackground:withObject: crée essentiellement un nouveau thread et exécute le code dessus.

Questions connexes