ios - 如何使用swift检查线程是否正在运行或完成

标签 ios iphone multithreading swift

我真的不知道如何快速检查..线程是否存活。

在我的例子中,在按钮单击事件上我正在创建新线程并启动一个 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/

相关文章:

java - 通过将函数移至其自己的线程来加速 Java 代码

ios - 自动属性合成不会合成协议(protocol)中声明的属性——哪个?

iphone - OpenCV.Framework 不为 armv7s 架构编译

iphone - iOS 配置文件 : Current Distribution Certificate

java - 其他线程中的忙循环延迟了 EDT 处理

c# - TextBlock 不会更新

ios - uitableview 标题中的单独点击手势识别器

objective-c - 如何保存这组图像?

ios - arm6 设备显示更高的内存使用率

ios - 将数据从 Iphone 传输到计算机