在主线程上停止 运行 的 DispatchQueue
Stop a DispatchQueue that is running on the main thread
我有这段代码:
DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
}
现在,我希望能够停止该线程的执行。我怎样才能阻止它被执行?例如,3 秒后,我决定不再执行它,所以我想停止它。
您可以使用 DispatchWorkItem
s。它们可以安排在 DispatchQueue
秒并在执行前取消。
let work = DispatchWorkItem(block: {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
})
DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay, execute: work)
work.cancel()
您可以使用一次性 DispatchSourceTimer
而不是 asyncAfter
var oneShot : DispatchSourceTimer!
oneShot = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
oneShot.scheduleOneshot(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay))
oneShot.setEventHandler {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
}
oneShot.setCancelHandler {
// do something after cancellation
}
oneShot.resume()
并用
取消执行
oneShot?.cancel()
oneShot = nil
我有这段代码:
DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay) {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
}
现在,我希望能够停止该线程的执行。我怎样才能阻止它被执行?例如,3 秒后,我决定不再执行它,所以我想停止它。
您可以使用 DispatchWorkItem
s。它们可以安排在 DispatchQueue
秒并在执行前取消。
let work = DispatchWorkItem(block: {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
})
DispatchQueue.main.asyncAfter(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay, execute: work)
work.cancel()
您可以使用一次性 DispatchSourceTimer
而不是 asyncAfter
var oneShot : DispatchSourceTimer!
oneShot = DispatchSource.makeTimerSource(queue: DispatchQueue.main)
oneShot.scheduleOneshot(deadline: .now() + (delay * Double(isDelayAccounted.hashValue)) + extraDelay))
oneShot.setEventHandler {
self.isShootingOnHold = false
self.shoot()
self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)
}
oneShot.setCancelHandler {
// do something after cancellation
}
oneShot.resume()
并用
取消执行oneShot?.cancel()
oneShot = nil