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 6361989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:50:57+00:00 2026-05-24T23:50:57+00:00

I am running some tasks in UI part of my application. While doing this

  • 0

I am running some tasks in UI part of my application.

While doing this lengthy task I need to show progress dialog so I am using AsysnTask to perform this.

When user pressed back button while progress bar going on , I need to cancel progress bar and I need to suspend that background work going on.

I am calling “dismiss()” on Progress Dialog so it’s dismissed.

I am calling “cancel(true)” on my AsysnTask object, I thought my backgroung operations got stopped , but still it is executing though I have called “cancel(true)”.

Then I have done some search on this issue and came to know that “cancel(true)” only called “onCancelled()” method of the AsyncTask.

I don’t know what I have to write in my onCancelled() method so that my background operations got stopped immediately.

My doInBackground() method something look like this:

      protected void doInBackground()
         {
                  Statement  1;
                  Statement  2;
                 Statement  3;
                 Statement  4;
                 Statement  5;
                 Statement  6;
                 Statement  6;
                 Statement  7;
                 Statement  8;
                 Statement  9;

          }

let say in above code If got request to cancel after “Statement 4” then I don’t want to execute reaming statements.

The above “doInBackground()” is just a example to make it understandable , In actual my “doInbackground()” method will have 100’s of statements and I cant predict when I can get the request to cancel the operation.

Please help me how to do that.

  • 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-05-24T23:50:57+00:00Added an answer on May 24, 2026 at 11:50 pm

    In java threads are never terminated prematurely. You can kill a thread using native API, but Java API does not allow this anymore (even though there are some functions left that expose this interface, but they are not implemented and just throw exceptions).

    Android exposes same philosophy, threads can not be terminated. You as developer is responsible for writing a thread in a such a way, so it reacts correctly when it is has been cancelled.

    In your case you need to implement doInBackground() so it checks whether thread has already been cancelled or not. For example:

    protected Void doInBackground()
    {
           if(isCancelled()) return null;
    
           Statement  1;
           Statement  2;
    
           if(isCancelled()) return null;
    
           Statement  3;
    
           if(isCancelled()) return null;
    
           Statement  9;
     }
    

    This is just an example, you can make a better implementation that checks thread state before executing next junk of its job.

    By the way isCancelled() was specifically designed for such use case. From documentation:

    public final boolean isCancelled ()

    Returns true if this task was cancelled before it completed normally.
    If you are calling cancel(boolean) on the task, the value returned by
    this method should be checked periodically from
    doInBackground(Object[]) to end the task as soon as possible
    .

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

Sidebar

Related Questions

Our Swing application performs some long-running tasks in a background thread using the excellent
I have a web-application. As part of this, I need users of the app
I'm using a thrid party Windows service that handles some automation tasks by running
The following output appears after running some rake tasks: Loaded suite /usr/bin/rake Started Finished
I am running some profiling tests, and usleep is an useful function. But while
I have a service running some different tasks in a loop until the service
I am trying to use powershell to script some tasks within internet explorer. This
As part of some build automation of running xUnit.net tests with MSBuild , I'm
I've inherited a website developed using Struts 1.2 that I need to make some
After running some usability tests, I found that participants opened a jQuery Lightbox to

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.