How to Terminate loop/thread

Here is part of my program. Loop in findDuplicates starts in background thread after button was pressed. Is there any way to stop/kill thread/loop by pressing another button?

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

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

Answers


You can return from background thread. create one member variable, initialize it with 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

  ... }
...
}

Need Your Help

If i add 1 year to Datepicker's endDate() its showing 2 months lesser

javascript datetimepicker

I am facing problem in adding 1 year exact to a date object(from date) and set it as endDate to my to datepicker. It is adding all perfect except months are less by 2. This is the code I have used ...

Creating a WCF PUT Rest service

c# wcf rest .net-4.0 put

I have a simple REST web service in WCF that is declared as follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.