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

时间:2015-08-13 06:09:35

标签: ios iphone multithreading swift

我真的不知道如何检查线程是否在swift中是否存在。

就我而言,在按钮点击事件中我创建新线程并启动NSTimer对象。我定期检查线程是否存活。 那么我该如何检查线程是否存活。

 if (bitmap.getWidth() >= bitmap.getHeight()){

                    bitmap = Bitmap.createBitmap(
                            bitmap,
                            0,
                            0,
                            bitmap.getHeight(),
                            bitmap.getHeight()
                    );

                }else{

                    bitmap = Bitmap.createBitmap(
                            bitmap,
                            0,
                            0,
                            bitmap.getWidth(),
                            bitmap.getWidth()
                    );
                }

如果可能,请提供示例或参考链接。

感谢,

1 个答案:

答案 0 :(得分:3)

您可以通过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")
}
相关问题