tags:

views:

77

answers:

3

Hi, 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");
}
+1  A: 

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

jessecurry
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.
+2  A: 

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

Ahruman
+1  A: 

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
Hmm, I've been looking into CFRunLoop to accomplish this ? Would NSOperation be easier or better design compared to CFRunLoop ?
For a GUI Cocoa app? Almost assuredly yes.
Joshua Nozzi