Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created February 3, 2016 06:53
Show Gist options
  • Select an option

  • Save onmyway133/3ad9c8c566465667762f to your computer and use it in GitHub Desktop.

Select an option

Save onmyway133/3ad9c8c566465667762f to your computer and use it in GitHub Desktop.

Revisions

  1. onmyway133 created this gist Feb 3, 2016.
    21 changes: 21 additions & 0 deletions PingThread.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    // from https://dl.dropboxusercontent.com/u/3820193/VbierClientUnderTheHood.pdf

    class PingThread : NSThread {
    var pingTaskIsRunning = false
    var semaphore = dispatch_semaphore_create(0)
    override func main() {
    while !self.cancelled {
    pingTaskIsRunning = true
    dispatch_async(dispatch_get_main_queue()) {
    self.pingTaskIsRunning = false
    dispatch_semaphore_signal(self.semaphore)
    }
    NSThread.sleepForTimeInterval(0.4)
    if pingTaskIsRunning {
    // warning here
    }
    dispatch_semaphore_wait(semaphore,
    DISPATCH_TIME_FOREVER)
    }
    }
    }