如何停止Iphone NSthread中主线程的后台线程? [英] How can i stop the background thread from the main thread in Iphone NSthread?

查看:57
本文介绍了如何停止Iphone NSthread中主线程的后台线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主线程正在检查某些内容时,我从程序中启动了一个后台线程.如果检查失败,则我想停止正在运行的后台线程,并使用新参数重新开始.

I start a background thread from my program while my main thread is checking something. If the checking goes false, I want to stop the running background thread and start it all over again with my new parameters.

我想停止它,因为该线程将下载图像,如果我不停止它并再次调用它,则要花费大量时间来下载新图像.有可能这样做吗?

I want to stop it coz the thread will be downloading images and if I doesnt stop it and call it again, it will take a lot of time for the new images to get downloaded. Is it possible to do so?

推荐答案

以我的经验,通常最好让线程自己优雅地退出.如果出现问题,将主线程设置为false的公共变量如何处理.第二个线程可以定期检查此变量,如果已将其设置为false,则知道应该清除并返回该变量.

In my experience, its usually best to let a thread exit gracefully by itself. How about having some common variable that the main thread could set to false if things go awry. The second thread could periodically check this variable and if it has been set to false, it knows it should clean up and return.

我认为您可以更优雅地使用条件变量,但是正确使用NSLock的方法应该行得通.

I think you could use condition variables in a more elegant fashion, but his approach with the proper use of NSLock should work.

这篇关于如何停止Iphone NSthread中主线程的后台线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆