Ideal way to cancel an executing AsyncTask
Asked Answered
I

9

109

I am running remote audio-file-fetching and audio file playback operations in a background thread using AsyncTask. A Cancellable progress bar is shown for the time the fetch operation runs.

I want to cancel/abort the AsyncTask run when the user cancels (decides against) the operation. What is the ideal way to handle such a case?

Impetuosity answered 29/4, 2010 at 6:8 Comment(0)
H
77

Just discovered that AlertDialogs's boolean cancel(...); I've been using everywhere actually does nothing. Great.
So...

public class MyTask extends AsyncTask<Void, Void, Void> {

    private volatile boolean running = true;
    private final ProgressDialog progressDialog;

    public MyTask(Context ctx) {
        progressDialog = gimmeOne(ctx);

        progressDialog.setCancelable(true);
        progressDialog.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // actually could set running = false; right here, but I'll
                // stick to contract.
                cancel(true);
            }
        });

    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected void onCancelled() {
        running = false;
    }

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

        while (running) {
            // does the hard work
        }
        return null;
    }

    // ...

}
Hyponasty answered 29/4, 2010 at 19:28 Comment(11)
additionally I called the cancel method on the AsyncTaskImpetuosity
instead of making a boolean flag for running , couldn't you remove it and make this while(!isCanceled())???Squilgee
From docs about onCancelled(): "Runs on the UI thread after cancel(boolean) is invoked and doInBackground(Object[]) has finished." This 'after' means that setting a flag in onCancelled and checking in doInBackground makes no sense.Midwifery
@Squilgee thats right, but this way background thread doesn't get interrupted, suppose the case when uploading image, the uploading process continues in background and we didn't get onPostExecute called.Cupronickel
@Midwifery The suggestion was to use isCancelled, which you can call from any thread. The onCancelled callback is different.Adrieneadrienne
@DanHulme I believe I referred to the code snippet provided in the answer, not to the confucius's comment (which is right).Midwifery
@Midwifery I see what you mean now. Yes, you're right that the answer won't work at all.Adrieneadrienne
I think this accepted answer does not work,keep it accepted is confusingTroostite
Yes, this answer doesn't work. In the doInBackground, replace while(running) with while(!isCancelled()) as others have said here in the comments.Irrepealable
This is answer is not correct....imagine a condition where you are not in a loop , but simply waiting for an http call to finish. At the time it entered running was true ..but once the running became false no effect on waiting and the prcess continues waiting.Intercalary
The method you need to implement to be aware of when it is cancelled is onCancelled(Void aVoid).Norris
C
76

If you're doing computations:

  • You have to check isCancelled() periodically.

If you're doing a HTTP request:

  • Save the instance of your HttpGet or HttpPost somewhere (eg. a public field).
  • After calling cancel, call request.abort(). This will cause IOException be thrown inside your doInBackground.

In my case, I had a connector class which I used in various AsyncTasks. To keep it simple, I added a new abortAllRequests method to that class and called this method directly after calling cancel.

Chicanery answered 10/6, 2012 at 23:3 Comment(6)
thank you, it works, but how to avoid the exception in this case?Goodkin
You must call HttpGet.abort() from a background thread or you'll get a android.os.NetworkOnMainThreadException.Smokeproof
@Chicanery If you're doing an HTTP request, shouldn't cancel(true) interrupt the request? From documentation: 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.Suppurative
HttpURLConnection.disconnect();Overword
If you have a cpu consuming operation in AsyncTask, so you must call cancel(true). I used it and it works.Sarmiento
what is request.abort()??Trilbi
W
20

The thing is that AsyncTask.cancel() call only calls the onCancel function in your task. This is where you want to handle the cancel request.

Here is a small task I use to trigger an update method

private class UpdateTask extends AsyncTask<Void, Void, Void> {

        private boolean running = true;

        @Override
        protected void onCancelled() {
            running = false;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            onUpdate();
        }

        @Override
        protected Void doInBackground(Void... params) {
             while(running) {
                 publishProgress();
             }
             return null;
        }
     }
Winburn answered 5/7, 2011 at 19:34 Comment(2)
This will work, but logically when you are waiting for server response, and you just did db operation, then is should reflect correct changes to your activity. I wrote a blog on that, see my answer.Hour
As mentioned in comments on the accepted answer, there is no need to create your own running flag. AsyncTask has an internal flag that is set when the task has been cancelled. Replace while (running) with while (!isCancelled()). developer.android.com/reference/android/os/… So in this simple case, you don't need onCancelled() override.Goosander
W
11

Simple: don't use an AsyncTask. AsyncTask is designed for short operations that end quickly (tens of seconds) and therefore do not need to be canceled. "Audio file playback" does not qualify. You don't even need a background thread for ordinary audio file playback.

Whitsunday answered 29/4, 2010 at 13:19 Comment(8)
are you suggesting we use regular java thread and "abort" the thread run using volatile boolean variable - the conventional Java way?Impetuosity
I don't know about volatile -- I tend to use java.util.concurrent objects for that sort of thing.Whitsunday
No offense Mike, but that's not an acceptable answer. AsyncTask has a cancel method, and it should work. As far as I can tell, it doesn't - but even if I'm doing it wrong, then there should be a right way to cancel a task. The method wouldn't exist otherwise. And even short tasks may need canceling - I have an Activity where it begins an AsyncTask immediately upon loading, and if the user hits back immediately after opening the task, they'll see a Force Close a second later when the task finishes but no context exists for it to use in its onPostExecute.Communist
@Klondike: I have no idea who "Mike" is. "but that's not an acceptable answer" -- you are welcome to your opinion. "AsyncTask has a cancel method, and it should work." -- canceling threads in Java has been a problem for ~15 years. It has nothing much to do with Android. With respect to your "Force Close" scenario, that can be solved by a boolean variable, which you test in onPostExecute() to see whether you should go ahead with the work.Whitsunday
Yikes, can't believe I said "Mike" - my apologies on that. Canceling threads in Java may have been a problem for 15 years, but if it's not reliable there shouldn't be a method in the Android SDK that has a boolean flag that implies the thread will be actively interrupted if it's that non-deterministic. It may as well not be there.Communist
I realize I'm bringing up an old topic, but I'm using AsyncTask for network calls. There take ~5s to complete, but I'd still like the user to have an option to cancel the non-critical ones if they choose. Are you saying AsyncTask.cancel() method is not reliable?Telephonist
@Tejaswi Yerukalapudi: It's more that it won't do anything automatically. See the accepted answer on this question.Whitsunday
You are supposed to check the isCancelled method periodically in your doInBackground on AsyncTask. It's right there in the docs: developer.android.com/reference/android/os/…Feathers
F
4

The only way to do it is by checking the value of the isCancelled() method and stopping playback when it returns true.

Footpath answered 3/5, 2010 at 19:34 Comment(0)
S
4

This is how I write my AsyncTask
the key point is add Thread.sleep(1);

@Override   protected Integer doInBackground(String... params) {

        Log.d(TAG, PRE + "url:" + params[0]);
        Log.d(TAG, PRE + "file name:" + params[1]);
        downloadPath = params[1];

        int returnCode = SUCCESS;
        FileOutputStream fos = null;
        try {
            URL url = new URL(params[0]);
            File file = new File(params[1]);
            fos = new FileOutputStream(file);

            long startTime = System.currentTimeMillis();
            URLConnection ucon = url.openConnection();
            InputStream is = ucon.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);

            byte[] data = new byte[10240]; 
            int nFinishSize = 0;
            while( bis.read(data, 0, 10240) != -1){
                fos.write(data, 0, 10240);
                nFinishSize += 10240;
                **Thread.sleep( 1 ); // this make cancel method work**
                this.publishProgress(nFinishSize);
            }              
            data = null;    
            Log.d(TAG, "download ready in"
                  + ((System.currentTimeMillis() - startTime) / 1000)
                  + " sec");

        } catch (IOException e) {
                Log.d(TAG, PRE + "Error: " + e);
                returnCode = FAIL;
        } catch (Exception e){
                 e.printStackTrace();           
        } finally{
            try {
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                Log.d(TAG, PRE + "Error: " + e);
                e.printStackTrace();
            }
        }

        return returnCode;
    }
Steadfast answered 2/6, 2011 at 9:10 Comment(1)
I found that simply calling cancel(true) on the async task and checking isCancelled() periodically does work, but depending on what your task is doing, can take up to 60 seconds before it gets interupted. Adding the Thread.sleep(1) enables it to get interupted immediately. (Async Task goes into a Wait state rather and isn't discarded immediately). Thanks for this.Nosy
F
0

Our global AsyncTask class variable

LongOperation LongOperationOdeme = new LongOperation();

And KEYCODE_BACK action which interrupt AsyncTask

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LongOperationOdeme.cancel(true);
        }
        return super.onKeyDown(keyCode, event);
    }

It works for me.

Fullfaced answered 11/2, 2013 at 10:21 Comment(0)
L
0

I don't like to force interrupt my async tasks with cancel(true) unnecessarily because they may have resources to be freed, such as closing sockets or file streams, writing data to the local database etc. On the other hand, I have faced situations in which the async task refuses to finish itself part of the time, for example sometimes when the main activity is being closed and I request the async task to finish from inside the activity's onPause() method. So it's not a matter of simply calling running = false. I have to go for a mixed solution: both call running = false, then giving the async task a few milliseconds to finish, and then call either cancel(false) or cancel(true).

if (backgroundTask != null) {
    backgroundTask.requestTermination();
    try {
        Thread.sleep((int)(0.5 * 1000));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    if (backgroundTask.getStatus() != AsyncTask.Status.FINISHED) {
        backgroundTask.cancel(false);
    }
    backgroundTask = null;
}

As a side result, after doInBackground() finishes, sometimes the onCancelled() method is called, and sometimes onPostExecute(). But at least the async task termination is guaranteed.

Lavonlavona answered 11/6, 2013 at 13:55 Comment(1)
Look like a race condition.Xebec
H
0

With reference to Yanchenko's answer on 29 April '10: Using a 'while(running)' approach is neat when your code under 'doInBackground' has to be executed multiple times during every execution of the AsyncTask. If your code under 'doInBackground' has to be executed only once per execution of the AsyncTask, wrapping all your code under 'doInBackground' in a 'while(running)' loop will not stop the background code (background thread) from running when the AsyncTask itself is cancelled, because the 'while(running)' condition will only be evaluated once all the code inside the while loop has been executed at least once. You should thus either (a.) break up your code under 'doInBackground' into multiple 'while(running)' blocks or (b.) perform numerous 'isCancelled' checks throughout your 'doInBackground' code, as explained under "Cancelling a task" at https://developer.android.com/reference/android/os/AsyncTask.html.

For option (a.) one can thus modify Yanchenko's answer as follows:

public class MyTask extends AsyncTask<Void, Void, Void> {

private volatile boolean running = true;

//...

@Override
protected void onCancelled() {
    running = false;
}

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

    // does the hard work

    while (running) {
        // part 1 of the hard work
    }

    while (running) {
        // part 2 of the hard work
    }

    // ...

    while (running) {
        // part x of the hard work
    }
    return null;
}

// ...

For option (b.) your code in 'doInBackground' will look something like this:

public class MyTask extends AsyncTask<Void, Void, Void> {

//...

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

    // part 1 of the hard work
    // ...
    if (isCancelled()) {return null;}

    // part 2 of the hard work
    // ...
    if (isCancelled()) {return null;}

    // ...

    // part x of the hard work
    // ...
    if (isCancelled()) {return null;}
}

// ...
Hotze answered 4/1, 2018 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.