I've seen some good information on how to keep an NSTask running in the background, although that's not entirely what I want to do. What I would like to do is periodically run an NSTask in the the background (like every 30 seconds), then kill it; here might be one example of what I would like to do:
NSTask *theTask = [ [ NSTask alloc ] init ];
NSPipe *taskPipe = [ [ NSPipe alloc ] init ];
[ theTask setStandardError:taskPipe ];
[ theTask setStandardOutput:taskPipe ];
[ theTask setLaunchPath:@"/bin/ls" ];
[ theTask setArguments:[ NSArray arrayWithObject:@"-l" ] ];
[ theTask launch ];
// Wait 30 seconds, then repeat the task
Maybe you can simply put in sleep the thread and wait 30 seconds inside a do loop :
do {
[ theTask launch ]; //Launch the Task
sleep(30); //Sleep/wait 30 seconds
} while (someCondition);
Otherwise you could use an NSTimer :
NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 30.0
target: self
selector:@selector(onTick:)
userInfo: nil repeats:YES];
- (void)onTick:(NSTimer *)timer {
//In this method that will be get called each 30 seconds,
//you have to put the action that you want to perform ...
[ theTask launch ];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With