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. PNG 24 with Gradient not showing correctly

Hi

I have a LinearLayout and I am setting a Background image.
I am using a PNG24, and the image has a gradient in it.
When I see the image on the computer the gradient is perfect, but when
I see it on the emulator or on a real phone, some colors are lost and
I see only like 10 colors, so I see lines and not a perfect gradient.
Is the emulator 'compressing' the images?
Should I make them in other format?

Thanks

Daniel
--~--~---------~--~----~------------~-------~--~----~

2. How to detect home screen?

I posted yesterday asking how to detect idle screen. I guess Android
uses different terms. So here goes again:

How can I detect the handset or emulator is at Home screen?

Thanks,

Sherry
--~--~---------~--~----~------------~-------~--~----~

3. Is it possible for an app icon to drag over and interact with a widget?

4. Is it possible for an app icon to drag over and interact with a widget?

5. how to load private font to widget from a remote view or from xml declaration - HELP

6. ActivityGroup and window token

7. Does Android support API to find out whether the SIM Card inserted is SIM or USIM ?