0

I have 30 threads (AsyncTask) executed in the same time. Before execute them, I will show progressdialog. After threads finish execution, I will dismiss progressdialog.

Any suggestion on How to dismiss ProgressDialog after finish all threads?

thalsharif
  • 357
  • 2
  • 6
  • 18

3 Answers3

1

If you are using exact 30 threads then initialze a variable with 30 and after every task completion decrease the count by one and also check if the variable is zero . if it is zero it indicate that all the tasks has been completed. so you can dismiss the ProgressDialog.

Ishu
  • 5,357
  • 4
  • 16
  • 17
  • one will need to run a for loop to continuously check the counter, which will block the UI thread and is not recommended. – Akhil Apr 01 '12 at 15:06
  • 1
    No just write it in onPostexecute() of your async task. so it will check just 30 time. i.e., once for every async task – Ishu Apr 01 '12 at 15:09
  • also I think the variable should be shared between all thread? – thalsharif Apr 01 '12 at 15:13
1

The best way to do this is use an AsyncTask() to execute your work and dismiss the dialog in onPOstExecute() of it. This will ensure that the task has completed and the ProgressDialog finished. If you still want to continue using Threads , you need to impplement a custom listener which is fired whenever a thread has completed, and you can maintain a counter of how many times it has been fired. I could have given you a code, better example is at this link: How to know if other threads have finished?

Community
  • 1
  • 1
Akhil
  • 13,888
  • 7
  • 35
  • 39
1

you can use a global class:

public abstract class Global{

 private static int counter = 30;
 private static ProgressDialog pd;
 private static Activity a;

 public static synchronized void updateCounter(){


 counter--;

 if(counter<=0){

    a.runOnUiThread(new Runnable() {

    @Override
    public void run() {

        pd.dismiss();
    }
   });

  }

 }

}

you need to use the "synchronized" because of the concurrent access of the threads.

And in your main activity start the ProgressDialog and initialize the variables:

Global.a = this;
Global.pd = ProgressDialog.show(this, "Tittle","Message ...", true);

and then start the threads.

On the end of each thread you can then call Global.updateCounter();

Vítor Marques
  • 349
  • 4
  • 11