当我需要等待线程完成时,我无法显示“等待”屏幕

时间:2014-07-04 12:18:42

标签: multithreading java-me

当任务需要一些时间才能完成时,我需要显示某种动画(进度指示器)。 这是我的等待屏幕的代码:

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;

    public class scrWaitForm extends Form {

        public static scrWaitForm create() {
            return new scrWaitForm();
        }
        private final Gauge gagProgressBar;
        private final StringItem strMensaje;

        protected scrWaitForm() {
            super("Procesando");
            this.gagProgressBar = new Gauge("", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
            this.gagProgressBar.setLayout(Item.LAYOUT_CENTER| Item.LAYOUT_VCENTER);
            this.append(gagProgressBar);
            this.strMensaje=new StringItem("Loading...", null);
            this.append(strMensaje);
        }
    }

这就是我展示它的方式:

    public void showWaitForm() {
        scrWaitForm frmWaitForm = scrWaitForm.create();
        mDisplay.setCurrent(frmWaitForm);
    }

如您所见,它非常简单。我只用一个量表。问题是如果我需要等待一个线程完成以便我可以在其他操作中使用某些操作结果(来自线程),则不会显示等待屏幕。但是,如果我只是在不等待线程完成的情况下调用等待屏幕,它就会按预期工作。

这是我最初的做法:

    thrLoadCustomers load = new thrLoadCustomers(rmsCustomers, url);
    Thread t = new Thread(load);
    showWaitForm()
    t.start();          
    try {
        t.join();
    } catch (InterruptedException ex) {

    }

但是在Mister Smith的帮助下,我最终使用了这样的东西:

WSResult result = new WSResult();

//Start thread here
new Thread(new LoadCustomersTask(result)).start();

//This is old school thread sync. 
synchronized(result){
    showWaitForm();
    while(!result.isCompleted()){
        result.wait();
    }
}

我做错了什么?你通常如何显示动画或其他屏幕,以便用户看到正在发生的事情并且不打算一遍又一遍地调用相同的动作。

提前致谢。

0 个答案:

没有答案
相关问题