Stop death pin in an infinite loop Target C

I am writing a simple timer program for myself in the c lens for my mac. The timer is counting correctly, but I get a spinning death pin. How can I make the pin go away? I think this is because I have an infinite loop, but there must be some way around it.

I have an IBAction that fires when a button is clicked (the button starts). And from there it calls another function that performs this work.

Here is my IBAction:

- (IBAction)timerStart:(id)sender { self.timerDidPause = NO; [self timerRunning]; } 

And here is timerRunning:

 - (void)timerRunning { for (;;) { usleep(1000000); if (self.timerDidPause == YES) { } else { if (self.seconds == 0) { if (self.minutes == 0) { [self timerDone]; break; } else { self.seconds = 59; self.minutes = self.minutes - 1; [self formatTimerLabel:self.hours :self.minutes :self.seconds]; } } else { self.seconds = self.seconds - 1; [self formatTimerLabel:self.hours :self.minutes :self.seconds]; } } } } 

In this function, the formatTimerLabel function is called like this:

 - (void)formatTimerLabel:(int)hours :(int)minutes :(int)seconds { NSString *minuteString = [[NSString alloc] init]; NSString *secondString = [[NSString alloc] init]; if (minutes < 10) { minuteString = [NSString stringWithFormat:@"0%d", minutes]; } else { minuteString = [NSString stringWithFormat:@"%d", minutes]; } if (seconds < 10) { secondString = [NSString stringWithFormat:@"0%d", seconds]; } else { secondString = [NSString stringWithFormat:@"%d", seconds]; } [self.timerLabel setStringValue:[NSString stringWithFormat:@"%d:%@:%@", hours, minuteString, secondString]]; [self.timerLabel display]; } 
+7
source share
1 answer

You are causing the user interface thread to hang with your loop. After a couple of seconds, the OS will switch the cursor to the pin.

You need to study NSTimer and the Timer Programming Guide to schedule a timer to run outside the user interface thread.

+7
source

All Articles