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. Google login service crashing

Hello,
I'm porting Android on imx51 board and I'm facing some problems with
Calendar an Contacts apllications which crah with the message: the
application has stopped unexpectedly.
I read somewhere that this could be related to the fact that those
apllications try to sync with the Google ones and they do this throw
Google Login Service which also crashes with the message : Unable to
start service Intent
{ cmp=com.google.android.googleapps/.GoogleLoginService }: not found
Can somebody please help?
Thanks,
Hadhami

-- 
unsubscribe: android-porting+unsubscr...@googlegroups.com
website: 

2. Help regarding Logging

Hi,
for my application I would like to be able to incorporate some good
Logging capabilities...
I would also like to use the networiking capabilitites for two
purposes :
- first I should be able to send a Logging Level from my server to my
client (Android app)... So the Logging Level should be adjustable
client-side
- secondly I must be able to read out the Log to send it to my server
through http...

I have seen there are two options in Android :
java.util.Log and java.util.Logging

The first one seems pretty straight forward but there are two issues
with that :
- can I programmatically read out the Log ? And if so how ?
- I have read that specific Logging Levels are automatically ignored
once an app has been deployed (like the verbose level)... Because of
the fact that I might want to have verbose-level logging from the
server I was wondering if it's able to actually de-ignore those
Levels ?

Also are there any tutorials reagrding these two packages ?
And what woul you, as an experienced developer, advise me to use of
those two methods ?

-- 

3. Basic program structure questions

4. The auto-generated R.java generation is invalid. Can't compile a new APK.

5. K9

6. Sim Card Indosat Unlimited 175rb/m

7. AVD which resembles the Nexus One?