Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AsyncTask won't stop when cancelled, why?

I've an AsyncTask that I shut down in the Activity's onPause lifecycle event, so it doesn't run when someone leaves the app, but it keeps going despite this. I added some tracing and this snippet shows the problem.

    Trace.d(TAG,"Task state: " + myTask.getStatus() );
    myTask.cancel(true);
    Trace.d(TAG,"Task state: " + myTask.getStatus() );

Outputs:

Task state: RUNNING
Task state: RUNNING

Why is the cancel() method not having any effect on the state of the task? I notice the docs say the cancel method will "attempt" to stop the task, but under what circumstances will it fail? The task is definitely running as it is outputting log output every ten seconds, and as you can see above its status is returned as running.

Update: I added tracing to show me the isCancelled() state as well and that DOES change. So the call to cancel(true) is changing the cancelled state from false to true, but apparently having no effect on the Status, or stopping the thread.

like image 930
Ollie C Avatar asked Feb 10 '11 16:02

Ollie C


2 Answers

Keep in mind that your Activity and your AsyncTask are two separate threads. So even if you cancel the AsyncTask, the code to cancel the task may not run yet! That's why you are seeing the AsyncTask still running even after fixing your bug. I don't think the status will change until doInBackground() in your AsyncTask has completed. (So make sure you're checking isCancelled() and returning early when you've been cancelled!)

like image 66
idbrii Avatar answered Oct 02 '22 06:10

idbrii


I dug deeper and remembered that my thread was calling a rather badly written method containing this, producing another thread inside the one I need to cancel:

public static void haveASleep(int milliseconds)
{
    try
    {
        Thread.sleep(milliseconds);
    }
    catch (InterruptedException ie)
    {
        Trace.w(TAG,"Sleep interrupted");
    }
}

So what was happening was that the AsyncTask thread calls this method to wait a while, and during the sleep the cancel() method call occurs on the AsyncTask, which causes an exception in the thread sleep. My code unhelpfully caught and absorbed that exception, instead of using it to shut down the AsyncTask.

The solution was to look for an exception on the sleep, and if it is thrown, to quietly exit the thread. The app now works as expected, BUT...

The status immediately after the cancel() method call remains RUNNING, but I suspect this is because at that moment it is still running, and the OS has not yet shut it down. I have no idea if that's true, but that's my best estimate.

like image 25
Ollie C Avatar answered Oct 02 '22 06:10

Ollie C