Iphone 如何停止当前正在执行的线程
情况是这样的-- 在我的应用程序中,有一个滚动视图,其中包含许多 MyCustomImageDownloaderController(包含图像视图,其中图像在下载后将被分配) 根据我们的要求,在进入页面时必须下载图像 滚动视图+(MyCustomImageDownloaderController 1、MyCustomImageDownloaderController 2、MyCustomImageDownloaderController 3…等) 假设我在上面滚动,Iphone 如何停止当前正在执行的线程,iphone,ios4,nsthread,Iphone,Ios4,Nsthread,情况是这样的-- 在我的应用程序中,有一个滚动视图,其中包含许多 MyCustomImageDownloaderController(包含图像视图,其中图像在下载后将被分配) 根据我们的要求,在进入页面时必须下载图像 滚动视图+(MyCustomImageDownloaderController 1、MyCustomImageDownloaderController 2、MyCustomImageDownloaderController 3…等) 假设我在上面滚动, 我转到第1页-->图像,因为
我转到第1页-->图像,因为它应该开始下载
我到达第2页-->图像,它应该开始下载…以此类推 如果我在第3页,并且之前页面的图像未加载,则应停止下载。 所以我试着用线程。。 在API上 现在我的MyCustomImageDownloaderController如下
-(void) startBackGroundThreadForPlaceImage:(NSURL *) url{
if(isImageDownloaded == NO){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//[self performSelectorInBackground:@selector(loadImageInBackground:) withObject:imageUrl];
myThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageInBackground:) object:imageUrl];
//[NSThread detachNewThreadSelector:@selector(loadImageInBackground:) toTarget:self withObject:imageUrl];
[myThread start];
NSLog(@"The current thread is %@ ", [[NSThread currentThread] name]);
[pool release];
}
}
现在,选择器在这里执行加载图像和指定给图像视图的工作
现在停止线程
-(void) stopBackgroundThread{
[myThread cancel];
//[[NSThread currentThread] cancel];
//if([[NSThread currentThread] isCancelled]) {
//[NSThread exit];
//}
[NSThread exit];
}
}
因此,我尝试了很多方法,但无法阻止线程之间的
基本上是使用三种方法中的任何一种实例化线程
1)在后台执行选择器
2)NSThread分离新线程
3)NSThread alloc..init with..
在前两种方法中,如何获取新创建线程的实例,以便我可以将其弯下腰
因为NSThread currentThread不提供该选项
在方法3中
myThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageInBackground:) object:imageUrl];
当我试着
[myThread cancel];
[NSThread exit];
它没有取消该线程
当我试着
[myThread cancel];
[NSThread exit];
它挂在当前屏幕上,,,我想它已经停止了主线程
请帮帮我
提前感谢*strong text*通常最好是要求线程停止,而不是强制它,这应该被视为最后的手段。因此,您应该经常检查“stop flag”,当设置该标志时,终止当前线程(在本例中,只需退出该方法)。您只需要在线程正在操作的类上提供一个属性,以便调用者可以要求线程停止
<> P> > C++和java。没有什么不同。基本上我需要在这个标志的循环检查中执行线程…如果标志改变退出方法将完成这个工作……但是你能详细说明吗?线程如何执行我的任务以及检查循环…请帮助我…谢谢