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不会停止线程,它只是设置取消标志。

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 the isCancelled method. Threads that support cancellation should periodically call the isCancelled 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/

相关文章:

ios - 在 IAP 收据验证中检测促销代码

ios - 如何快速实现类调配?

ios - 有什么方法可以使 dispatch_queue_t 在单线程中工作?

swift - block 没有真正执行?

objective-c - 命令行 Objective-C 程序如何创建其主 NSThread 线程?

ios - cordova.file.* - 所有位置在 iOS 中都是空的。适用于安卓

ios - NSDictionaryResultType 与 NSManagedObjectResultType

ios - 从 Swift 中的特定线程运行代码

objective-c - 在 Objective-C 中暂停执行 WHILE 循环的最佳方法

ios - Xamarin Native 链接 Paypal 失败