Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2011, 06:19 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
hehe0613 is on a distinguished road
Default Stopping a thread before it ends

Hello All

My application loads images from internet in a detached thread, as follow :

[self performSelectorInBackground: @selector(displayProductsImages withObject: theProduct];

- (void) displayProductsImages: (Product *) theProduct
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// code that download images using dataWithContentsOfURL function
.....
[pool release];
}

It works fine, but in some circumstances I need to stop the detached thread before it ends naturally. Unfortunately I cannot find any simple function that would allow me to stop this thread ?

I tried using cancelPreviousPerformRequestsWithTarget but no success !

Any idea would be greatly appreciated !
hehe0613 is offline   Reply With Quote
Old 01-12-2011, 06:41 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

If you force an end to a thread, then there would be no opportunity for that thread to clean up its resource allocations. So instead of trying to force an end to the thread, look for ways to change the thread so that it periodically checks for a notification from the main UI thread that it should stop. Then the worker thread can gracefully stop what it is doing and exit.
RLScott is offline   Reply With Quote
Old 01-12-2011, 06:58 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
hehe0613 is on a distinguished road
Default

Quote:
Originally Posted by RLScott View Post
If you force an end to a thread, then there would be no opportunity for that thread to clean up its resource allocations.
Right, as mentioned in the documentation

Quote:
Originally Posted by RLScott View Post
So instead of trying to force an end to the thread, look for ways to change the thread so that it periodically checks for a notification from the main UI thread that it should stop. Then the worker thread can gracefully stop what it is doing and exit.
Hmm yes, but I will have to count the number of actives threads I think.

I currently have a view controller which launches this loading image background thread.
In another view controller (parent of the above view controller), users can click a button which will have for effect to load new images in the child view controller.
If the user clicks fast on the button, for instance 3 times, what I observe is :
- the image which corresponds to the first time the user clicked is displayed
- the second image is then displayed once loaded
- the third image is then displayed once loaded

In the above case, I need to stop loading the images of the 2 first clicks to only display the third. Should I count the number of active threads and display image only if the last thread ?

something like :

- (void) displayProductsImages: (Product *) theProduct
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

nbThreads = countTheNbOfActiveThreadsInThisViewController();

if(nbThreads == 1)
{
// code that download images using dataWithContentsOfURL function
.....
// code that displays the loaded image
....
}

[pool release];
}

If the above seems correct, I need to be able to count the numbers of active threads, is there a way to do that ?

Thanks again for your time, really appreciate ...
J
hehe0613 is offline   Reply With Quote
Old 01-12-2011, 07:09 AM   #4 (permalink)
1026 Development
 
Join Date: Jul 2008
Age: 23
Posts: 1,094
Jeremy1026 is on a distinguished road
Send a message via AIM to Jeremy1026
Default

I'd do an if statement.

Code:
 - (void) thread {
NSAutoRelasePool *pool...
if (!threadIsRunning) {
threadIsRunning = YES;
// Download Image
// Download Finishes
threadIs Running = NO;
}
[pool release];
}
That assumes you have properly declared threadIsRunning as a BOOL in your header file.
Jeremy1026 is offline   Reply With Quote
Old 01-12-2011, 07:19 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
hehe0613 is on a distinguished road
Default

Quote:
Originally Posted by Jeremy1026 View Post
I'd do an if statement.

Code:
 - (void) thread {
NSAutoRelasePool *pool...
if (!threadIsRunning) {
threadIsRunning = YES;
// Download Image
// Download Finishes
threadIs Running = NO;
}
[pool release];
}
That assumes you have properly declared threadIsRunning as a BOOL in your header file.
Thanks for your answer, but what you mentioned will have for effect to load ONLY the first image, I need to display only the latest, not the first !
Am I wrong ?
hehe0613 is offline   Reply With Quote
Old 01-12-2011, 07:24 AM   #6 (permalink)
1026 Development
 
Join Date: Jul 2008
Age: 23
Posts: 1,094
Jeremy1026 is on a distinguished road
Send a message via AIM to Jeremy1026
Default

it would limit it to one load at a time, so if the user presses the button 10 times quickly, only the 1st and 10th image would load.

Last edited by Jeremy1026; 01-12-2011 at 07:29 AM.
Jeremy1026 is offline   Reply With Quote
Old 01-12-2011, 07:30 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 19
hehe0613 is on a distinguished road
Default

Quote:
Originally Posted by Jeremy1026 View Post
it would limit it to one laod at a time, so if the user presses the button 10 times, only the 1st and 10th image would load.
the 10th image would be displayed only if the first has been completely downloaded.

If the user presses the button only 2 times, very fast, the second image will not display ... since the first is still downloading.
hehe0613 is offline   Reply With Quote
Reply

Bookmarks

Tags
background, kill, pool, selector, thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 363
9 members and 354 guests
7twenty7, blueorb, iAppDeveloper, iGamesDev, Mah6447, Morrisone, mottdog, sacha1996, Touchmint
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:36 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0