情景就像this--如何停止当前执行的线程
在我的应用程序有一个与
MyCustomImageDownloaderController的很多实例(含其中图像下载后要分配imageViews)的滚动视图。
根据我们的要求,当我们进入页面时必须下载图像。
滚动型+(MyCustomImageDownloaderController1,MyCustomImageDownloaderController2,MyCustomImageDownloaderController3 ....等)
比方说,我正在滚动就可以了,
我伸手第1页 - >为它的形象应该开始下载
我达到第2页 - >图像,它应该开始下载...等等
如果我在页面3和图像为先前的网页,如果没有dowloaded,他们应该停止下载。
所以我使用线程试过.. 的API ..
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender{
Step 1) calculated currentPageNumber
Step 2) started thread for downloading image with url for this currentPage
//startBackGroundThreadForPlaceImage:(NSURL *) url
Step 3)stopped thread for previous page , if that is still running
}
现在我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];
}
-(BOOL) isThreadRunning{
return [myThread isExecuting];
}
所以我尝试了很多东西,但不能停止线程..
之间
基本上一次使用三种方法中的任何一种实例化线程1)在背景
2)执行选择NSThread分离新线程
3)NSThread alloc..init与..
在第2种方法如何获得新创建的线程的情况下,这样我就可以弯腰了,
为NSThread currentThread行得给这
方法3,
myThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageInBackground:) object:imageUrl];
当我试图
[myThread cancel];
它没有取消线程,,
,当我试图
[NSThread exit];
它挂在当前屏幕上,,,,我想它已经停止主线程
请帮我
在此先感谢* 强文本 *
所以基本上我需要在检查该标志的循环内执行线程......如果标志更改退出方法将完成这项工作......但是可以详细说明一下,线程将如何执行我的任务以及检查循环...请帮助我.. 谢谢! –