3

I am using an Android AsyncTask in order to download files from a server. when files are downloaded, I am trying to kill the AsyncTask.

protected void onPostExecute(Void result) {
    MyTask.cancel(true);
}

But it stills running (I can see it from the Debug window in eclipse). How to kill the AsyncTask?

user1471575
  • 731
  • 2
  • 15
  • 23
  • 1
    possible duplicate of [Android - Cancel AsyncTask Forcefully](http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully) and http://stackoverflow.com/questions/4959628/android-asynctask-wont-stop-when-cancelled-why and http://stackoverflow.com/questions/2735102/ideal-way-to-cancel-an-executing-asynctask and http://stackoverflow.com/questions/6039158/android-cancel-async-task and others. – CommonsWare Aug 21 '12 at 13:54

2 Answers2

3

When the AsyncTask is finished running, onPostExecute() will be called. The thread will die. The garbage collector will clean it up. You don't have to do anything. What you're doing is redundant.

Other than that, calling "Cancel" sends and interrupt signal to the thread. If your process can be interrupted, it will stop blocking and continue to execute. You then have to call isCancelled() in doInBackground() and return from the method if isCancelled() is true. After which, onCanceled() and onPostExecute() will be called and the thread will die on it's own like normal.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • But the threads is always running even after the onPostExecute() has been called: Thread [<18> AsyncTask #1] (Running) – user1471575 Aug 21 '12 at 14:00
  • 2
    @user1471575 Yes because it is more expensive to create new threads than having an idle thread. You should not worry about that. – zapl Aug 21 '12 at 14:01
  • @ zapl: but i will have: Thread [<18> AsyncTask #1] (Running), Thread [<20> AsyncTask #2] (Running)..... Is that ok? – user1471575 Aug 21 '12 at 14:03
  • Check with [DDMS](http://developer.android.com/tools/debugging/ddms.html) what they are doing – zapl Aug 21 '12 at 14:05
  • 1
    Threads have a tendency to stick around until the GC decides to collect them. If they just keep building over the course of your app and they're never collected, it might imply a memory leak of some sort. Test your app intensively to determine if they're being collected or not. – DeeV Aug 21 '12 at 14:08
  • @zapl: They are in a Wait status – user1471575 Aug 21 '12 at 14:11
  • 1
    Check the [AsyncTask source](http://www.grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/os/AsyncTask.java#AsyncTask.SerialExecutor). AsyncTasks are executed by an executor that tries to resuse the threads. That behavior is different for other API levels but you should consider the actual thread to be managed for you and not try to stop it manually (which you could do if you manage to interrupt it). – zapl Aug 21 '12 at 14:42
2

There is some documentation from here about canceling an AsyncTask, which may be relevant:

A task can be cancelled at any time by invoking cancel(boolean).

Invoking this method will cause subsequent calls to isCancelled() to return true.

After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.

To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)"

Community
  • 1
  • 1
Abner Niu
  • 405
  • 3
  • 7
  • 1
    In the future, please do not just copy-paste documentation here. Instead, link that documentation as a comment, so that if the docs change, your post isn't out-of-date. – Richard J. Ross III Aug 21 '12 at 22:40