0
Private static ProgressDialog loading;

public void downloadData(){
    Thread t = new Thread(){
        public void run(){
          //download stuff
        }
        handler.sendEmptyMessage(0);
    };
    t.start();
    try{
        t.join();
    }catch(InterruptedException ignore){}
}

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        loading.dismiss();
    }
};

When I call donloadData without using t.join() it displays the ProgressDialog.

However, when using t.join(), the t thread seems to execute correctly but the ProgressDialog does not show.

Why is the ProgressDialog not showing?

Any suggestions on what to change so that I can use t.join() and display the ProgressDialog?

4

2 回答 2

0

t.join方法将阻止当前线程直到t thread完成它的工作。

尝试这个:

Private static ProgressDialog loading;

public void downloadData(){
    Thread t = new Thread(){
        public void run(){
          //download stuff

         //edit: when finish down load and send dismiss message
         handler.sendEmptyMessage(0);
        }
        //handler.sendEmptyMessage(0);
    };

    //edit: before start make the dialog show
    loading.show();

    t.start();
    //edit: the join method is not necessary
    try{
        t.join();
    }catch(InterruptedException ignore){}
}

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        loading.dismiss();
    }
};

上面的代码可能会解决你的问题。

于 2012-04-12T01:46:11.643 回答
0

join() 是一个死亡电话。如果线程阻塞或卡住,在其上调用 join() 可确保卡住有效地扩展到调用线程。

如果您可以侥幸逃脱,请不要使用 join() 。特别是不要通过在 start() 之后直接调用它来等待来自另一个线程的结果。双重——尤其是不要在 GUI 事件处理程序中使用它。

于 2012-04-12T02:03:40.170 回答