Should AsyncTask.cancel(true) work?

by Nathan » Wed, 21 Apr 2010 04:43:04 GMT


Sponsored Links
 Cause it doesn't.

I have a service running an Asynctask to do some work.

I bind to it from an activity and call a cancel method. The service,
in turn, calls AsyncTask.cancel(true);

AyncTask.cancel returns true. Nonetheless, the thread is still running
happily and still doing the things in doInBackGround, sending
notifications along the way.

There is no sign that it attempted to kill the thread.

I am not using NDK calls or anything that I think should stop if from
taking down the thread.

What am I doing wrong? Or is this a known issue?

Nathan

public final boolean cancel (boolean mayInterruptIfRunning)

Since: API Level 3
Attempts to cancel execution of this task. This attempt will fail if
the task has already completed, already been cancelled, or could not
be cancelled for some other reason. If successful, and this task has
not started when cancel is called, this task should never run. If the
task has already started, then the mayInterruptIfRunning parameter
determines whether the thread executing this task should be
interrupted in an attempt to stop the task.
Parameters
mayInterruptIfRunning   true if the thread executing this task should be
interrupted; otherwise, in-progress tasks are allowed to complete.
Returns
false if the task could not be cancelled, typically because it has
already completed normally; true otherwise

--



Should AsyncTask.cancel(true) work?

by Nathan » Wed, 21 Apr 2010 05:04:56 GMT


 Because it isn't.

AsynTask.cancel(true) is called from a service on its AsyncTask

It returns true.

After the call, AsyncTask.iscancelled() is true.

Nonetheless, the thread is still running happily and performing the
tasks in doinBackground as if nothing had happened. I can see this all
in the debugger.

Any ideas? Is this a known issue?

I see no evidence that this is working according to its documentation,
below.

Nathan

public final boolean cancel (boolean mayInterruptIfRunning)

Since: API Level 3
Attempts to cancel execution of this task. This attempt will fail if
the task has already completed, already been cancelled, or could not
be cancelled for some other reason. If successful, and this task has
not started when cancel is called, this task should never run. If the
task has already started, then the mayInterruptIfRunning parameter
determines whether the thread executing this task should be
interrupted in an attempt to stop the task.
Parameters
mayInterruptIfRunning   true if the thread executing this task should be
interrupted; otherwise, in-progress tasks are allowed to complete.
Returns
false if the task could not be cancelled, typically because it has
already completed normally; true otherwise

--


Sponsored Links


Should AsyncTask.cancel(true) work?

by Dianne Hackborn » Wed, 21 Apr 2010 05:12:27 GMT


 Killing threads is horribly bad and not something anyone should do.  You
need to check in your task if it has been canceled, and return if so.




>



Should AsyncTask.cancel(true) work?

by Nathan » Wed, 21 Apr 2010 06:16:52 GMT


 


I don't necessarily disagree. But that is what the documentation says
it does. So someone who wrote the documentation thought killing
threads was fine and the person who implemented it thought differently
and ignored the parameter? They should probably talk.

When Android kills the service, which it can do at any time without
warning, does it kill the Async threads?  I'm assuming so. So it
sounds like I do have to handle thread death whether I initiate it or
not.

Nathan

--



Should AsyncTask.cancel(true) work?

by Mark Murphy » Wed, 21 Apr 2010 06:25:04 GMT


 




I am fairly certain the person who wrote the documentation and who wrote
the class is the same person, and I'm not aware that he suffers from
multiple personality disorder. :-)

The documentation, as you quoted earlier, says:

"If the task has already started, then the mayInterruptIfRunning
parameter determines whether the thread executing this task should be
interrupted in an attempt to stop the task."

That does not imply, however, that the thread is interruptible. That's a
Java thing, not an AsyncTask thing.

I'm with Ms. Hackborn on this one -- I avoid designs that require Java
threads to be stopped.


If by "kills the service" you mean "destroys the service", then no, I'm
reasonably certain the threads are not killed. The AsyncTask will run to
completion regardless of the status of the component that started is.
Leastways, I'm rather sure this is what happens with activities, and I'm
not aware of any differences with respect to services in this regard.
AsyncTask maintains a thread pool, so even when the task is complete,
the thread does not necessarily terminate.

If by "kills the service" you mean "terminates the process", then yes,
the thread will go away when the process goes away.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://twitter.com/commonsguy 

Android Training...At Your Office:  http://commonsware.com/training 

--



Should AsyncTask.cancel(true) work?

by abisai rangel » Wed, 21 Apr 2010 06:50:56 GMT


 New HP Slate Photos here ----> Website with
photos< http://bizoppoffer.com/x/0/1320/6428/> ;








>



Should AsyncTask.cancel(true) work?

by Nathan » Wed, 21 Apr 2010 07:11:05 GMT


 


OK, that's what I needed to check on. The task is doing IO so it
should get interrupted eventually, but I could be catching it as an
IOException and continuing.

At any rate, the graceful cancellation works.



OK, now I'm worried. I see a possible runaway task here with no way to
cancel.

I should of course call AsyncTask.cancel() in onDestroy()

But Ms. Hackborn has stated in previous threads that onDestroy() won't
necessarily be called when a service is destroyed.

If the process is alway terminated when a service is destroyed without
warning, then I won't worry about the runaway task.

Nathan

--



Should AsyncTask.cancel(true) work?

by Dianne Hackborn » Wed, 21 Apr 2010 07:20:55 GMT


 



onDestroy() won't be called only in the case where your entirely process is
killed to free up memory.

-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

--



Should AsyncTask.cancel(true) work?

by Nathan » Wed, 21 Apr 2010 07:28:25 GMT


 


OK, no worry about runaway threads then. But I will be watching the
memory.

Nathan

--



Other Threads

1. Listview in a listview without ExpandableList

Hello,

I am trying to make a kind of library representing in a first list the
authors and in a sublist their books. All these authors and books come
from xml files, an then all the books are linked to a pdf to read
them.

I do not wish to use an ExpandableList, because I don't want all the
author to remain visible when I click on the first list, I just want
to see the second list.

My problem is that I don't know how to make the listview within the
listview, do I have to create an activity for each  author ? And how
are the author linked to their books ?


Thanks, sorry for my English

-- 

2. Linked folder in project

Hi,

In my project, it is required to use certain files from folder which
is outside src\ location. With eclipse i could achieve this using
linked resources(folder),. But same project has to be compiled using
ant script, so i am not sure how to solve this dependency.

Thanks,
amit

-- 

3. adapterview with arraylist

4. Nonton Bareng Google I/O

5. [WTAsk] Cara Root

6. [WTA] beli HH, mohon sarannya.

7. [WTA] droid x tidak bisa root