iOS,如何重新启动NSThread?
要重新启动NSSthread,我将使用以下代码:iOS,如何重新启动NSThread?,ios,nsthread,Ios,Nsthread,要重新启动NSSthread,我将使用以下代码: - (IBAction)listen:(id)sender { thread = [[NSThread alloc] initWithTarget:self selector:@selector(listen1) object:nil]; [thread start]; [thread cancel]; [thread start]; } 但当我构建并按下这个按钮时,我收到了错误:“尝试再次启动线程” 你能帮我吗?方法-cancel不会停止线程
- (IBAction)listen:(id)sender {
thread = [[NSThread alloc] initWithTarget:self selector:@selector(listen1) object:nil];
[thread start];
[thread cancel];
[thread start];
}
但当我构建并按下这个按钮时,我收到了错误:“尝试再次启动线程”
你能帮我吗?方法
-cancel
不会停止线程,它只是设置了cancel标志
更改接收器的取消状态,以指示它应该退出
-(作废)取消
此方法的语义与用于NSOperation
对象的语义相同。此方法设置接收器中的状态信息,然后通过isCancelled
方法反映该信息。支持取消的线程应该定期调用isCancelled
方法来确定该线程是否已被取消,如果已被取消则退出
通过调用[NSThread exit]
只能从线程本身停止线程
顺便问一下,你想达到什么目标?启动单独的线程几乎从来都不是好主意,启动同一个线程两次是两次错误。删除第一个[线程启动];在[线程取消]之前;并在此处添加listen1方法进行说明