Hello,
I have the below program in Objective-C++ for IOPSNotificationCreateRunLoopSource.
But, I am not able to receive power source change events without the infinite do { } while(!done) loop in startThread() function.
Please suggest a way to get rind of the infinite loop to receive power source change events.
#import "PowerSourceInfoWorker.h"
@implementation PowerSourceInfoWorker
-(void) dealloc {
[self stopThread];
[super dealloc];
}
-(void) startThread {
BOOL done = NO;
do {
runLoopSource = (CFRunLoopSourceRef)IOPSNotificationCreateRunLoopSource(powerSourceChange, self);
if(runLoopSource) {
NSLog(@"Waiting for power source change........ ");
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
}
int result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, YES);
done = (result == kCFRunLoopRunStopped) || (result == kCFRunLoopRunFinished);
} while (!done);
}
-(void) stopThread {
if(runLoopSource) {
CFRunLoopSourceInvalidate(runLoopSource);
CFRelease(runLoopSource);
}
}
void powerSourceChange(void* context) {
NSLog(@"Power Source has changed");
}
int main(int argc, const char * argv[]) {
NSLog(@"Power Source Change detection");
PowerSourceInfoWorker *psiWorker = [[PowerSourceInfoWorker alloc]init];
NSLog(@"before startThread");
[psiWorker startThread];
}
@end