在Swift中停止并启动NSThread

时间:2014-12-16 09:48:32

标签: ios swift

我有一个带有runLoop内部线程主要功能的线程正常工作。此runLoop位于while循环内,如果取消为false,将循环迭代。

public override func main() {

    super.main()

    var runLoop: NSRunLoop = NSRunLoop.currentRunLoop()
    do {
        runLoop.run()
    } while(!self.cancelled)
    NSLog("Thread stopped")
    NSThread.exit()
}

当我为线程设置cancel标志(通过NSTreah.cancel()方法)时,线程似乎成功结束,因为我在控制台中 Thread stopped ,但是当我尝试启动时再一次,我总是得到attempt to start the thread again。如何在不收到此错误的情况下重新启动线程?

1 个答案:

答案 0 :(得分:4)

您无法重新启动线程。这是多线程编程的基本概念,一个线程运行一次,当它被停止时它不会重启。只需创建一个新对象。

相关问题