So I have a method that performs a loop and because of the way my program works i'm calling this method using,
我有一个方法来执行一个循环由于我的程序的工作方式我用,
[self performSelectorInBackground:@selector(myMethod:) withObject:arg];
And the method itself,
和方法本身,
- (void)myMethod:(id)arg {
//...
for (//Loop Conditions) {
if (stopMorse) {
break;
}
//Do stuff
}
//...
}
Now I would like to allow the user to stop this loop from running, so is there a way of killing/stopping the thread.
现在,我希望允许用户停止这个循环的运行,那么是否存在一种杀死/停止线程的方法。
I've tried adding a BOOL but the problem is that it doesn't seem to get updated because it's on a separate thread. What is the best way to stop this loop from running?
我尝试过添加BOOL,但问题是它似乎没有得到更新,因为它是在一个单独的线程上。阻止这个循环运行的最好方法是什么?
1 个解决方案
#1
4
You shouldn't forcibly kill/stop the thread for reasons outlined in this answer to a similar question, and this answer to another similar question.
你不应该因为一个类似问题的答案和另一个类似问题的答案中所列出的原因而强行关闭/停止线程。
It should be possible to check a boolean as you are doing, and there shouldn't be a problem with another thread setting the value of the boolean. Can you post the code that is attempting to set the boolean?
应该可以检查一个布尔值,而另一个线程设置布尔值的值也不应该有问题。你能张贴试图设置布尔值的代码吗?
#1
4
You shouldn't forcibly kill/stop the thread for reasons outlined in this answer to a similar question, and this answer to another similar question.
你不应该因为一个类似问题的答案和另一个类似问题的答案中所列出的原因而强行关闭/停止线程。
It should be possible to check a boolean as you are doing, and there shouldn't be a problem with another thread setting the value of the boolean. Can you post the code that is attempting to set the boolean?
应该可以检查一个布尔值,而另一个线程设置布尔值的值也不应该有问题。你能张贴试图设置布尔值的代码吗?