Ollie C
Ollie C

Reputation: 28519

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.

Upvotes: 16

Views: 14457

Answers (5)

tpbapp
tpbapp

Reputation: 2506

thread.cancel(true);

Only sets a variable in the AsyncTask class. What you need to do is implement a loop into your background task if you haven't already. Then check and break the loop if cancel is true for each iteration.

@Override  
protected Void doInBackground(Void... params) {  

    synchronized(this) {

        for(int i = 0; i < 9000; i++) {

            if(thread.isCancelled()) break;

            // do stuff

        }

    }

}

Then just set it to cancelled whenever you need it to stop. It will then stop before the following iteration.

@Override
public void onStop() {

    super.onStop();

    thread.cancel(true);

}

@Override
public void onDestroy() {

    super.onDestroy();

    thread.cancel(true);

}

Upvotes: 2

blub
blub

Reputation: 9207

Another possible pitfall to check if you're running into this problem:

Make sure you are not starting two AsyncTasks.

Upvotes: 0

idbrii
idbrii

Reputation: 11966

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!)

Upvotes: 3

Ollie C
Ollie C

Reputation: 28519

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.

Upvotes: 2

Mojo Risin
Mojo Risin

Reputation: 8142

Even if you know how cancel works it's very hard to determine in what state your thread will be stopped. So I suggest to implement your own safe way to stop your thread, so you'll be sure in what state you are stopping and you be sure there are no memory leaks, etc. From more info how to stop a thread in java in safe way check this thread How to abort a thread in a fast and clean way in java?

Upvotes: 0

Related Questions