views:

1767

answers:

4

Hi I would like to pause the execution of a function in an cocoa project. I dont want to use sleep() because the function needs to resume after user interaction. I also want to avoid doing this with multiple calls to sleep.

Thanks for your responses. Ok I started the code while waiting for some answers. I then realized that sleep or pause would not be usefull to me because it freeses my whole program. I think I might have to do some threading. Here is the situation:

I have a program that uses coreplot. I also use it to debug and develop algorithms so I do lots of plots while the data is being processed (ie in the midfle of the code but I need the flexibility to put it anywhaere so I cant separate my function). I was able to do this with NSRunAlertPanel but having a message box like that doesnt make it very presentable and I cant do much with the main window while an alert is open.

I hope I am not too confusing with my explanation but if I am ill try to one line it here:

I would like to interact with my cocoa interface while one of my functions is stopped in the middle of what it is doing.

A: 

In C you can use the pause() function in <unistd.h>. This causes the calling program to suspend until it receives a signal, at which point the pause call will return and your program will continue (or call a signal handler; depending on what signal was received).

Dave Rigby
That won't really help with the “resume after user interaction” part.
Peter Hosey
+2  A: 

It seems more like you are interested in reacting to user events rather than "pausing" the function. You would probably want to put the code that you want to execute into another function that is called as a result of the user's actions.

Critical Failure
I need to react to user events but my function must stop what it is doing until user event. I cant breakdown my function I need to put those anywhere I want
yan bellavance
+1  A: 

So it sounds like you want to break the function into two parts; the bit that happens before the sleep and the bit that happens afterward. Before going to sleep, register for a notification that calls the "after" code, and can be triggered by the UI (by an IBAction connected to whatever UI element). Now instead of calling sleep(), run the run loop for the period you want to go to sleep for, then after that has returned post the "after" notification. In the "after" code, remove the object as an observer for that notification. Now, whichever happens first - the time runs out or the user interrupts you - you get to run the "after" code.

Graham Lee
+1  A: 

Its sounds to me like you're looking for -NSRunLoop runUntilDate:

Apple's Docs: runUntilDate

This code will cause the execution within your method to pause but still let other events like timers and user input occur:

while ( functionShouldPause )
{
     [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}

Switching functionShouldPause back to false will allow the rest of the method to execute.

JimDusseau