要重新启动 NSSthread,我使用以下代码:
- (IBAction)listen:(id)sender {
thread = [[NSThread alloc] initWithTarget:self selector:@selector(listen1) object:nil];
[thread start];
[thread cancel];
[thread start];
}
但是当我构建并按下此按钮时,我收到错误:“尝试再次启动线程” 你能帮我吗?
最佳答案
方法-cancel
不会停止线程,它只是设置取消标志。
Changes the cancelled state of the receiver to indicate that it should exit.
- (void)cancel
The semantics of this method are the same as those used for the
NSOperation
object. This method sets state information in the receiver that is then reflected by theisCancelled
method. Threads that support cancellation should periodically call theisCancelled
method to determine if the thread has in fact been cancelled, and exit if it has been.
您只能通过调用[NSThread exit]
从线程内部停止线程。
顺便问一下,你想实现什么目标?启动单独的线程几乎从来都不是一个好主意,两次启动同一个线程就是两次错误。
关于iOS,如何重启NSThread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25379606/