Well .. I'm in situation like this: I have a function that starts 3 asynchronous threads. Each thread do something wich take some time. When some thread finish first, I need it to stop the other 2, but I dont know how to do it (yet).
嗯. .我现在的情况是:我有一个启动3个异步线程的函数。每条线都需要一些时间。当一些线程先完成时,我需要它来停止另外两个线程,但是我还不知道怎么做。
My code:
我的代码:
class SomeController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
threads();
}
func threads(){
let operationQueue = NSOperationQueue();
operationQueue.addOperationWithBlock(
{
let thread = NSThread.currentThread();
let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;
NSThread.sleepForTimeInterval(30);
println("Task #1 completed on thread #\(threadNumber)");
})
operationQueue.addOperationWithBlock(
{
let thread = NSThread.currentThread();
let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;
NSThread.sleepForTimeInterval(20);
println("Task #2 completed on thread #\(threadNumber)");
})
operationQueue.addOperationWithBlock(
{
let thread = NSThread.currentThread();
let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;
NSThread.sleepForTimeInterval(5);
println("Task #3 completed on thread #\(threadNumber)");
})
}}
Any Swift help or advice will be apperciated :)
任何迅速的帮助或建议将被告知:)
2 个解决方案
#1
3
This isn't really a question about canceling an NSThread
. Rather, it's a question of how to cancel an NSOperation
. In this case, it's relatively easy to cancel all the operations since you're creating a single NSOperationQueue
for the sole purpose of executing your three blocks. Just send the operationQueue a cancelAllOperations message:
这并不是关于取消NSThread的问题。而是如何取消ns操作的问题。在这种情况下,取消所有操作相对容易,因为您正在创建一个NSOperationQueue,惟一的目的是执行三个块。只需发送操作队列一个cancelAllOperations消息:
operationQueue.cancelAllOperations()
The unfortunate part is that operation canceling is really cooperative, so within the operation you have to periodically check isCancelled
and terminate as required:
不幸的是,取消操作是非常合作的,因此在操作中,您必须定期检查是否被取消,并按要求终止:
var operation3:NSOperation?
operation3 = operationQueue.addOperationWithBlock {
let thread = NSThread.currentThread()
let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue
var timeout = 5
while timeout-- > 0 && !operation3.isCancelled {
NSThread.sleepForTimeInterval(1)
}
println("Task #3 completed on thread #\(threadNumber)")
operationQueue.cancelAllOperations()
}
#2
1
If you create an operation with addOperationWithBlock, you can cancel it all you like, it has no effect. If you want to cancel an operation, I recommend not using a block, but subclassing NSOperation. The task that is getting executed must check manually when it's cancelled and finish the task; you can't do that with addOperationWithBlock.
如果您使用addOperationWithBlock创建一个操作,您可以取消它,它没有任何效果。如果您想取消操作,我建议不使用块,而是子类化NSOperation。正在执行的任务必须在取消时手动检查并完成任务;你不能用addOperationWithBlock那样做。
#1
3
This isn't really a question about canceling an NSThread
. Rather, it's a question of how to cancel an NSOperation
. In this case, it's relatively easy to cancel all the operations since you're creating a single NSOperationQueue
for the sole purpose of executing your three blocks. Just send the operationQueue a cancelAllOperations message:
这并不是关于取消NSThread的问题。而是如何取消ns操作的问题。在这种情况下,取消所有操作相对容易,因为您正在创建一个NSOperationQueue,惟一的目的是执行三个块。只需发送操作队列一个cancelAllOperations消息:
operationQueue.cancelAllOperations()
The unfortunate part is that operation canceling is really cooperative, so within the operation you have to periodically check isCancelled
and terminate as required:
不幸的是,取消操作是非常合作的,因此在操作中,您必须定期检查是否被取消,并按要求终止:
var operation3:NSOperation?
operation3 = operationQueue.addOperationWithBlock {
let thread = NSThread.currentThread()
let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue
var timeout = 5
while timeout-- > 0 && !operation3.isCancelled {
NSThread.sleepForTimeInterval(1)
}
println("Task #3 completed on thread #\(threadNumber)")
operationQueue.cancelAllOperations()
}
#2
1
If you create an operation with addOperationWithBlock, you can cancel it all you like, it has no effect. If you want to cancel an operation, I recommend not using a block, but subclassing NSOperation. The task that is getting executed must check manually when it's cancelled and finish the task; you can't do that with addOperationWithBlock.
如果您使用addOperationWithBlock创建一个操作,您可以取消它,它没有任何效果。如果您想取消操作,我建议不使用块,而是子类化NSOperation。正在执行的任务必须在取消时手动检查并完成任务;你不能用addOperationWithBlock那样做。