简体   繁体   English

完成所有线程后如何关闭ProgressDialog?

[英]How to dismiss ProgressDialog after finish all threads?

I have 30 threads (AsyncTask) executed in the same time. 我有30个线程(AsyncTask)在同一时间执行。 Before execute them, I will show progressdialog. 在执行它们之前,我将显示progressdialog。 After threads finish execution, I will dismiss progressdialog. 线程完成执行后,我将关闭进度对话框。

Any suggestion on How to dismiss ProgressDialog after finish all threads? 关于完成所有线程后如何关闭ProgressDialog的任何建议?

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 . 如果使用的线程数恰好是30,则用30初始化变量,并在每次任务完成后将计数减1,并检查变量是否为零。 if it is zero it indicate that all the tasks has been completed. 如果为零,则表明所有任务已完成。 so you can dismiss the ProgressDialog. 因此您可以关闭ProgressDialog。

The best way to do this is use an AsyncTask() to execute your work and dismiss the dialog in onPOstExecute() of it. 最好的方法是使用AsyncTask()执行工作,并在其onPOstExecute()中关闭对话框。 This will ensure that the task has completed and the ProgressDialog finished. 这将确保任务已完成并且ProgressDialog已完成。 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. 如果您仍要继续使用Threads,则需要实现一个自定义侦听器,该侦听器在线程完成时会被触发,并且您可以维护一个被触发了多少次的计数器。 I could have given you a code, better example is at this link: How to know if other threads have finished? 我可以给你一个代码,更好的例子是在此链接: 如何知道其他线程是否完成?

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: 在您的主要活动中,启动ProgressDialog并初始化变量:

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(); 在每个线程的最后,您可以调用Global.updateCounter();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM