【问题标题】:Progress bar freezing on window 8窗口 8 上的进度条冻结
【发布时间】:2014-05-13 09:23:16
【问题描述】:

您好,我正在使用一个框架,在该框架上加载一个对话框。单例对话框包含一个进度条。 我创建了自己的摇摆工人。我正在控制摇摆工人的构造方法(其功能类似于 doInBackground 方法)。 我正在尝试从外部类调用 hidedialog。对话框有时会卡住。我该如何解决。在win7上运行正常,但在win8上失败。

  public class LoadingProgressDialogSingleton extends com.manu.scpoweb.common.ds.client.swing.dialog.Dialog {
    protected static LoadingProgressDialogSingleton oneAndOnlyProgressDialog = null;
    JProgressBar progressBar = null;
    DFULoadStatusBean dfuLoadStatusBean = null;
    double progress;
    int progressComplete = 0;
    BusinessObjectUserContext uc = null;
    CopyProgressBarWorker copyProgressBarWorker = null;
    ProgressBarWorker progressBarWorker = null;
    public static synchronized LoadingProgressDialogSingleton getProgressDialog(IApplicationContext iApplicationContext, ClientDataManager dataManager){
        if (oneAndOnlyProgressDialog == null){
            oneAndOnlyProgressDialog = new LoadingProgressDialogSingleton(iApplicationContext.getParentFrame(),
                    iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Loading"), Boolean.TRUE);

            oneAndOnlyProgressDialog.progressBar = new JProgressBar(0, 100);
            oneAndOnlyProgressDialog.progressBar.setValue(0);
            oneAndOnlyProgressDialog.progressBar.setPreferredSize(new Dimension(175, 20));
            oneAndOnlyProgressDialog.progressBar.setStringPainted(true);
            oneAndOnlyProgressDialog.progressBar.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));

            JLabel label = new JLabel(iApplicationContext.getResource(APMConstants.RESOURCE_BUNDLE, "ProgressBar.Reloading"));

            JPanel center_panel = new JPanel();
            center_panel.setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 1;
            gbc.insets = new Insets(2, 6, 2, 6);
            center_panel.add(label, gbc);
            gbc.gridy = 1;
            center_panel.add(oneAndOnlyProgressDialog.progressBar, gbc);
            gbc.gridy = 2;
            center_panel.add(Box.createVerticalGlue(), gbc);

            oneAndOnlyProgressDialog.getContentPane().add(center_panel, BorderLayout.CENTER);
            oneAndOnlyProgressDialog.setLocationRelativeTo(null); // center on screen
            oneAndOnlyProgressDialog.setPreferredSize(new Dimension(350, 107));
            oneAndOnlyProgressDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

            oneAndOnlyProgressDialog.uc = new BusinessObjectUserContext(iApplicationContext.getUser().getUserName(),
                               iApplicationContext.getUserLocale());
            oneAndOnlyProgressDialog.uc.setAttributes(dataManager.getUserContext().getUserPreferences());
        }
        return oneAndOnlyProgressDialog;

    }
    public void showDialog(java.util.List<AttributeUUID> promoOfferKeysOnly){

        final Thread showDialogThread = new Thread(new Runnable() {
                public void run() {
                    toFront();
                    show();
                }
            });
        showDialogThread.start();

        if (promoOfferKeysOnly != null && promoOfferKeysOnly.size() > 0 ) {
           progressBarWorker = new ProgressBarWorker();
           BusinessObjectUserContext uc = new BusinessObjectUserContext(oneAndOnlyProgressDialog.uc.getUserName(),
                   oneAndOnlyProgressDialog.uc.getLocale());
           uc.setAttributes(oneAndOnlyProgressDialog.uc.getUserPreferences());
           progressBarWorker.setBusinessObjectUserContext(uc);
           progressBarWorker.setDataKey(promoOfferKeysOnly.get(0));
           progressBarWorker.start();
        }
    }

    public static synchronized void hideDialog(){
        if (oneAndOnlyProgressDialog != null){
            ProgressBarWorker worker = oneAndOnlyProgressDialog.progressBarWorker;
            CopyProgressBarWorker copyWorker = oneAndOnlyProgressDialog.copyProgressBarWorker;
            oneAndOnlyProgressDialog.dispose();

            if(oneAndOnlyProgressDialog.isShowing())
            oneAndOnlyProgressDialog.setVisible(false);
            oneAndOnlyProgressDialog = null;
            if (worker!=null){
                worker.setProgressComplete();
            }
            if (copyWorker!=null){
                copyWorker.setCopyCompleted();
            }
        }
    }
    protected LoadingProgressDialogSingleton(Frame iParent, String iTitle, boolean iModal) {
        super(iParent, iTitle, iModal);
    }
    class ProgressBarWorker extends SwingWorker {
        BusinessObjectUserContext uc = null;
        AttributeUUID dataKey = null;

        public AttributeUUID getDataKey() {
            return dataKey;
        }

        public void setDataKey(AttributeUUID dataKey) {
            this.dataKey = dataKey;
        }
        public void setProgressComplete(){
            progressComplete = 100;
        }
        public Object construct() {
            progressComplete = 0;
            EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
            if (queue instanceof ManuEventQueue) {
                try {
                    JAASUtilities.runAs(((ManuEventQueue) Toolkit.getDefaultToolkit().getSystemEventQueue()).getSubject(),
                            new PrivilegedExceptionAction() {
                                public Object run() throws Exception {
                                    long totalDFUsCnt = 0;
                                    long basePriceLoadedCnt = 0;
                                    long baseCostLoadedCnt = 0;
                                    long udasLoadedCnt = 0;
                                    long histLoadedCnt = 0;
                                    long fcstLoadedCnt = 0;
                                    progressBar.setValue(5);
                                    //Initialize progress property.
                                    while (progressComplete < 5) {
                                        if (Thread.currentThread().isInterrupted())
                                            break;
                                        if (dataKey != null) {
                                            dfuLoadStatusBean = APMController.getDFULoadStatus(dataKey, uc);
                                            if (dfuLoadStatusBean == null || !dfuLoadStatusBean.isStrated()) {
                                                Thread.currentThread().sleep(5000);
                                                continue;
                                            } else {
                                                if (dfuLoadStatusBean.getNumberOfDFUsInOffer() != null && totalDFUsCnt != dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond()) {
                                                    totalDFUsCnt = dfuLoadStatusBean.getNumberOfDFUsInOffer().getSecond();
                                                    progressBar.setValue(10);
                                                }
                                                if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair() != null && (baseCostLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getFirst()) {
                                                        baseCostLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        baseCostLoadedCnt = dfuLoadStatusBean.getDfuBaseCostStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair() != null && (basePriceLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getFirst()) {
                                                        basePriceLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        basePriceLoadedCnt = dfuLoadStatusBean.getDfuBasePriceStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair() != null && (udasLoadedCnt != totalDFUsCnt)) {`enter code here
                                                    if (dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getFirst()) {
                                                        udasLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;

                                                    } else {
                                                        udasLoadedCnt = dfuLoadStatusBean.getDfusUDAStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair() != null && (histLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getFirst()) {
                                                        histLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        histLoadedCnt = dfuLoadStatusBean.getDfusHISTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair() != null && (fcstLoadedCnt != totalDFUsCnt)) {
                                                    if (dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getFirst()) {
                                                        fcstLoadedCnt = totalDFUsCnt;
                                                        progressComplete++;
                                                    } else {
                                                        fcstLoadedCnt = dfuLoadStatusBean.getDfusFCSTStatusAndCountPair().getSecond();
                                                    }
                                                }
                                                if (totalDFUsCnt != 0.0)
                                                    progress = (((baseCostLoadedCnt / totalDFUsCnt * 80) + (basePriceLoadedCnt / totalDFUsCnt * 80) +
                                                            (udasLoadedCnt / totalDFUsCnt * 80) + (histLoadedCnt / totalDFUsCnt * 80) +
                                                            (fcstLoadedCnt / totalDFUsCnt * 80)) / 500d);

                                                if (progress != 0.0) {
                                                    progressBar.setValue((int) (progress * 100));
                                                    Thread.currentThread().sleep(500);
                                                }
                                            }
                                        }
                                    }
                                    return null;
                                }
                            });
                } catch (PrivilegedActionException e) {
                    e.printStackTrace();
                }
            }
            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }
        public void finished() {
          }

        public void setBusinessObjectUserContext(BusinessObjectUserContext iUc) {
            uc = iUc;
        }
    }
   }
}

【问题讨论】:

  • 这有点像试图从 EventDispatch 线程以外的不同线程在 UI 上完成某些事情的味道。在处理文档中未明确标记为线程安全的任何内容时,您应该确保使用类似 SwingUtilities.invokeLater() 的内容。
  • 是的,你的权利可能不是以线程安全的方式编写的。我们在这里做的错误是从 swing worker 的构造(doINBackground)更新进度条。我有 emodev 代码,但问题仍然存在

标签: java swing swingworker


【解决方案1】:

根据您所说的“卡住”的含义,我的代码似乎因为关联的计时器被垃圾收集而停止了。

换句话说,我必须将计时器变量移动到一个可以保留其实例的对象。

在我移动变量之前,没有运行时错误,它会停止工作。

【讨论】:

  • 感谢您的回复。我想要处理进度条和对话框。当调用 dispose 方法时,这可能不会发生。我不认为这与收集的任何组件有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多