gpt4 book ai didi

iOS,如何重启NSThread?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:04:06 24 4
gpt4 key购买 nike

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

- (IBAction)listen:(id)sender {
thread = [[NSThread alloc] initWithTarget:self selector:@selector(listen1) object:nil];
[thread start];
[thread cancel];
[thread start];
}

但是当我构建并按下这个按钮时,我收到了错误:“attempt to start the thread again”你能帮帮我吗?

最佳答案

-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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com