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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:06+00:00 2026-05-27T17:52:06+00:00

I’d like to do the following in the right order: – Do a long

  • 0

I’d like to do the following in the right order:
– Do a long task while showing a progress dialog
– And then use the result of this calculation.

However, to show a progress dialog I seem to be forced to use a Thread. But if I use a thread, the calculation is done before my long task is finished.

What is the solution ?

Both solutions that were proposed unfortunately don’t work for me. I will try to be more thorough.
Taking ProfSmiles’ code, here is what I’d like:

final Handler mHandler = new Handler(); //create the handler
String mResults; //the results of the operation (change the type if needbe)

final Runnable mDoSomething = new Runnable() 
{
public void run()
{
    returnResultsToUi(); //what happens when it's done
}
};
protected void doActionBackground()
{
t = new Thread()
{
    public void run()
    {
        mResults = doSomethingThatTakesALongTime(); //the backbone of this entire thing
        mHandler.post(mDoSomething);
    }
};
t.start();
}
public void returnResultsToUi()
{
//you can call UI ops safely in here
try
{
    //close the spinner
    loading.setProgress(1);
    loading.dismiss();
}
catch (Exception e)
{
    Log.w("error","couldn't close loading properly");
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

loading = new ProgressDialog(this); //create and then set up the spinner
loading.setMax(1);
loading.setTitle("Loading...");
loading.setMessage("Doing something.");
loading.setCancelable(true);
loading.setIndeterminate(false);
loading.setOnCancelListener(cancelLoad()); //remember to make this method or change setCancelable(false)
loading.setProgress(0);
loading.show();

doActionBackground(); //start the background process
doSomethingAfter(mResults);

The problem is that doSomethingAfter is called while doActionBackground is running, but mResults is not set yet and its value is null. Is there a way to force the thread to finish before calling doSomethingAfter? Like a function waitTillFinished(takesALongTime) that can be cancelled.

  • 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-27T17:52:07+00:00Added an answer on May 27, 2026 at 5:52 pm

    The solution is to use AsyncTask with ProgressBar.

    Here’s example1, example2

    Update:

    +1 yes this is the exact solution in android to implement Threading concept. Implement the below scenario to implement your solution:

    1. show progress bar inside onPreExecute() method.
    2. do all the background task inside the doInBackground() method.
    3. And display kind of operations can be implemented inside the onPostExecute() method.

    The best example suits my explanation is here: http://www.technotalkative.com/android-load-image-from-web/ , sorry for my personal blog link but i am sure this helps user to understand and implement the AsyncTask concept.

    • Also read about processes and threads here (and about AsyncTask in particular).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.