Having a process sleep for some time
Sometimes you might need to have your process sleep for some time before or after executing or have it run repeatedly at a set interval. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to have a process sleep.
Strategy to resize a picture is fairly simple: First, we need to determine what interval we want to sleep for. Second, we need determine if we want the process to sleep before or after executing it's tasks. Third, we need to determine if we want to run repeatedly. Finally, we must change the method header of the method we want to call.
Change the method header by adding a parameter ( in bold below ) :
- ( void ) someMethod : ( NSTimer * ) timer
Here are actual code samples:
To have a process sleep for some time, we will be using the NSTimer object. The NSTimer object will take certain parameters. We will be dealing with three parameters: scheduledTimerWithTimeInterval ( the sleep time in seconds ), @selector ( the method name to execute ), and repeats ( if you want it to repeat). In this example, we will be running a method called "newMethod" every 5 seconds.
[NSTimer scheduledTimerWithTimeInterval : 5.0f target : self selector : @selector( newMethod : ) userInfo : nil repeats : YES ] ;