Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:47:37+00:00 2026-06-08T13:47:37+00:00

How does the .cancel(boolean) method of a running AsyncTask work? Here is the documentation:

  • 0

How does the .cancel(boolean) method of a running AsyncTask work? Here is the documentation:

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.

Calling this method will result in onCancelled(Object) being invoked on the UI thread after doInBackground(Object[]) returns. Calling this method guarantees that onPostExecute(Object) is never invoked. After invoking this method, you should check the value returned by isCancelled() periodically from doInBackground(Object[]) to finish the task as early as possible.

The second paragraph implies that it is up to the programmer to return from the doInBackground method as soon as possible after cancel being called (by checking isCancelled() regularly). But in testing, my AsyncTask (doInBackground method) appears to stop straight away even if I don’t do this, after cancel is called. Could anyone explain this? (Logcat doesn’t show anything happening when cancel is called, btw).

protected Void doInBackground(Void... V) {
            for (int v=3;v>0;v--){
                publishProgress(v);
                try {Thread.sleep(1500);} catch (InterruptedException e) {}
            } 
            return null;
         }

Also, this happens regardless of the supplied value of mayInrerruptIfRunning, so if it could be explained where this value comes in, that would also be very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:47:39+00:00Added an answer on June 8, 2026 at 1:47 pm

    There are a few core methods (“blocking methods”), such as Thread.sleep(), which themselves check for thread interruption and throw the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why below code does not work: $('button[name=publish]', 'button[name=cancel]').live('click', function(){ alert('ddddd'); }); The html is
Here I am using XmlPullParser to parse the document below. It does not work
When using the FormClosing event, why does the code e.Cancel = true; work, but
WPF's Window.ShowDialog method returns a nullable boolean. So does CommonDialog.ShowDialog . Now, I understand
How should I correctly use CancelEventAgrs/CancelEventHandler? Does checking e.Cancel will return always the right
Does the G'MIC (Grey's Magic Image Converter) library work on iOS ? what steps
Does anybody know why this function, when passed an invalid date (e.g. timestamp) to
the cancel option on selectable does the following: Prevents selecting if you start on
I'd like to cancel a .load() operation, when the load() does not return in
Does anything like this exist? What I am looking for is a control that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.