Skip to content

Instantly share code, notes, and snippets.

@proactive-solutions
Last active November 16, 2016 13:25
Show Gist options
  • Save proactive-solutions/62d31ff13aec246f2d6ace554cc87417 to your computer and use it in GitHub Desktop.
Save proactive-solutions/62d31ff13aec246f2d6ace554cc87417 to your computer and use it in GitHub Desktop.
@interface PingThread : NSThread
@property (nonatomic, assign) BOOL pingTaskIsRunning;
@property (nonatomic, strong) dispatch_semaphore_t semaphore;
@end
@implementation PingThread
- (void)main {
self.semaphore = dispatch_semaphore_create(0);
self.pingTaskIsRunning = NO;
while (NO == self.isCancelled) {
self.pingTaskIsRunning = YES;
dispatch_async(dispatch_get_main_queue(), ^{
self.pingTaskIsRunning = false;
dispatch_semaphore_signal(self.semaphore);
});
[NSThread sleepForTimeInterval:0.4];
if (YES == self.pingTaskIsRunning) {
NSLog(@"handle the operation here");
}
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment