我真的不知道如何快速检查..线程是否存活。
在我的例子中,在按钮单击事件上我正在创建新线程并启动一个 NSTimer 对象。我需要定期检查线程是否还活着。 那么我如何检查线程是否存在。
var objDS= DeviceStatus()
let thread = NSThread(target: objDS, selector: "checkDeviceStatus", object: nil)
如果可能,请提供示例或一些引用链接。
谢谢,
最佳答案
可以通过Thread的执行状态属性来判断
正在执行
var executing: Bool { get } // true if the receiver is executing, otherwise false.
完成
var finished: Bool { get } //true if the receiver has finished execution, otherwise false.
已取消
var cancelled: Bool { get } //true if the receiver has been cancelled, otherwise false.
例子:
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")
}
关于ios - 如何使用swift检查线程是否正在运行或完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31980660/