Flavian Hautbois
Flavian Hautbois

Reputation: 3060

Threads in android and processes in the right order

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.

Upvotes: 0

Views: 323

Answers (2)

Grace B
Grace B

Reputation: 1406

You can use Handlers to do long-running processes in the background and return the result to the UI thread. For example;

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

Upvotes: 0

hovanessyan
hovanessyan

Reputation: 31463

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

Upvotes: 3

Related Questions