1

The basic idea I have is to click on one button and enter an infinite loop. I plan to click on another button to stop and get out of this loop. Trouble is once I get into the infinite loop, my second click is never detected so I can't get out. Any idea's on how I can get this to work ? Thanks a ton.

-(IBAction) startButton {
  while (1) { 
    // code
  }     
}

-(IBAction) stopButton {
  NSLog(@" out of loop now");
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
ZionKing
  • 326
  • 1
  • 4
  • 16

3 Answers3

2

If you can’t use a timer, you need to use a background thread, NSOperation or Grand Central Dispatch task.

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
1

What do you want your loop to do? Maybe you could use an NSTimer.

jessecurry
  • 22,068
  • 8
  • 52
  • 44
  • I am constantly collecting data and saving it to a file once I get into this loop. And the data source does not really stop, so I need to collect some data and then when I decide to stop, I want to click on this stop button and have the file closed and saved. So NSTimer won't work. – ZionKing Aug 13 '10 at 21:19
1

Why not use NSOperation and NSOperationQueue? Each trip through the loop, you can check if it isCanceled and break. That way, the main thread (on which your UI updates and responds) won't freeze up and your app won't beach ball.

The important thing to realize is, if you tie up the main thread in a loop, you won't get further events until the loop ends, which means no button-click-to-cancel.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135