Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS,如何重新启动NSThread?_Ios_Nsthread - Fatal编程技术网

iOS,如何重新启动NSThread?

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不会停止线程

要重新启动NSSthread,我将使用以下代码:

- (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方法进行说明