sia
sia

Reputation: 1910

How to check if thread is running or completed using swift

I'm really do not know how to check .. whether a thread is alive or not in swift.

In my case, on button click event I'm creating new thread and also starting a NSTimer object. At regular interval i need to check if thread is alive or not. So how do i check if thread is alive or not.

var objDS= DeviceStatus()
let thread = NSThread(target: objDS, selector: "checkDeviceStatus", object: nil)

If possible please provide a example or some reference link.

thanks,

Upvotes: 3

Views: 2462

Answers (1)

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

You can determine it through the Thread’s execution state properties

executing

var executing: Bool { get } // true if the receiver is executing, otherwise false.

finished

var finished: Bool { get } //true if the receiver has finished execution, otherwise false.

cancelled

var cancelled: Bool { get } //true if the receiver has been cancelled, otherwise false.

Example:

var objDS= DeviceStatus()
let thread: NSThread = NSThread(target: objDS, selector: "checkDeviceStatus", object: nil)

if thread.executing{
    println("executing")
}

if thread.finished{
    println("finished")
}

if thread.cancelled{
    println("cancelled")
}

Upvotes: 5

Related Questions