0

我有一个启动并运行线程 A 的操作侦听器。当操作完成时,线程 A 停止并且对话窗口关闭。

我创建了一个未知的简单进度条。主要是因为没有办法获得平均操作时间。时间取决于各种因素。所以,我将 TOTAL_TIME 设置得相当高。

我想在对话框关闭之前以某种方式停止进度条线程。

进度条线程

 class ProgressBarThread implements IRunnableWithProgress {
  private static final int TOTAL_TIME = 5000;
  private static final int INCREMENT = 1000;

  public ProgressBarThread() {

  }

  public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
     monitor.beginTask("Sending Plot", IProgressMonitor.UNKNOWN);
     for (int total = 0; total < TOTAL_TIME ; total += INCREMENT) {
        Thread.sleep(INCREMENT);
        monitor.worked(INCREMENT);
        if (total == TOTAL_TIME / 2) monitor.subTask("Please be patient... Operation should finish soon.");
    }
    monitor.done();

  }

OperationListener - 当操作返回时关闭窗口

 public abstract class MyOperationListener implements InterfaceAIFOperationListener {

  AplotPlotterDialog w = null;

  public MyOperationListener(AplotPlotterDialog win) {
     w = win;
  } 

  public void startOperation(String startMessage) {
     Display.getDefault().asyncExec(new Runnable() {
        public void run() {
           w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT));
           w.recursiveSetEnabled(getShell(), getShell().getEnabled());
           w.getShell().setEnabled(!getShell().getEnabled());
        }
     });
  } 

  public void endOperation() {
     try {
        endOperationImpl();
     }
     finally {
        Display.getDefault().asyncExec(new Runnable() {
           public void run() {
              w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
              w.recursiveSetEnabled(getShell(), true);
              w.getShell().setEnabled(!getShell().getEnabled());
              w.close();
           }
        });
     }
  } 
  abstract protected void endOperationImpl();

}

启动 ProgressBar 的方法

  public void startProgressBar() {
  try {
     new ProgressMonitorDialog(getShell()).run(true, false,
        new ProgressBarThread());
  } 
  catch (InvocationTargetException e) {
     MessageDialog.openError(getShell(), "Error", e.getMessage());
  }  
  catch (InterruptedException e) {
     MessageDialog.openInformation(getShell(), "Cancelled", e.getMessage());
  }

}

操作方法和 ProgressBar 从哪里开始

 public void startPrintOperation() {
  Display.getDefault().asyncExec(new Runnable() {
     public void run() {
        startProgressBar();
     }
  });
  final ArrayList<PlotData> datasetData =  AplotPlotDataModel.getInstance().getPlotDataArray();
  plotOp = new AplotPlotOperation(appReg.getString("aplot.message.PLOTTING"),datasetData, getPlottersData(), getSpinnerValue(), session);
  plotOp.addOperationListener(new MyOperationListener(this) {
     public void endOperationImpl() {
        try {
           plotResults = (ArrayList) plotOp.getPlotResults();
           for (int i = 0; i < plotResults.size(); ++i) {
                            AplotResultsDataModel.getInstance().addEntry((AplotPlotResultsParser.DatasetPlotResult) plotResults.get(i));
           } // end for
        }
        catch (Exception e) {
           e.printStackTrace();
        }
        finally {
           plotOp.removeOperationListener(this);
           plotOp = null;
           Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                 baseDialog.removeAllTableRows();
                 AplotPlotDataModel.getInstance().clearPlotDataArray();
              }
           });
        }
     }
  });
  session.queueOperation(plotOp);
} 

- - - - - - - - - - - - 编辑 - - - - - - - - -

这就是我尝试实施@Serious 建议的方式。

对 ProgressBar 类所做的更改

class ProgressBarThread implements IRunnableWithProgress  {
  private static final int TOTAL_TIME = 10000;
  private static final int INCREMENT = 1000;
  private Condition done = null;

  public ProgressBarThread(Condition done) {
     this.done = done;
  }

  public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException {
     monitor.beginTask("Creating PDF File(s)", IProgressMonitor.UNKNOWN);
     for (int total = 0; total < TOTAL_TIME ; total += INCREMENT) {
        if (done.await(INCREMENT, TimeUnit.MILLISECONDS))
        {
           break;
        }
        monitor.worked(INCREMENT);
        if (total == TOTAL_TIME / 2) monitor.subTask("Please be patient... Operation should finish soon.");
     }
    monitor.done();
  }
}

这是对 MyOperationLister 类的更改

public abstract class MyOperationListener implements InterfaceAIFOperationListener {
  AplotCreatePDFDialog w = null;
  Condition done = null;

  public MyOperationListener(AplotCreatePDFDialog win) {
     w = win;
     Lock lock = new ReentrantLock();
     done = lock.newCondition();
  } 

  public void startOperation(String startMessage) {
     Display.getDefault().asyncExec(new Runnable() {
        public void run() {
     startProgressBar();
        }
     });
  }

  public void startProgressBar() {
     try {
        new ProgressMonitorDialog(getShell()).run(true, false,
        new ProgressBarThread(done));
     } 
     catch (InvocationTargetException e) {
        MessageDialog.openError(getShell(), "Error", e.getMessage());
     }  
     catch (InterruptedException e) {
        MessageDialog.openInformation(getShell(), "Cancelled", e.getMessage());
     }
  }

  public void endOperation() {
     try {
        endOperationImpl();
     }
     finally {
        Display.getDefault().asyncExec(new Runnable() {
           public void run() {
              w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
              w.recursiveSetEnabled(getShell(), true);
              w.getShell().setEnabled(!getShell().getEnabled());
              done.signal();
              w.close();
           }
        });
     }
  } 
  abstract protected void endOperationImpl();
} 

我从对话类中删除了 startProgressBar 方法。我还从启动操作侦听器的方法中删除了 startProgressBar

 private void startSavePdfOperation() {
  //Display.getDefault().asyncExec(new Runnable() {
  //   public void run() {
  //      startProgressBar();
   //  }
  //});
  saveOp = new AplotSaveOperation(appReg.getString("aplot.message.SAVETOPDF"), "PDF", session);
  saveOp.addOperationListener(new MyOperationListener(this) {<-- this is the start operation listener call.

结果:我得到一个错误对话框而不是 ProgressBar 对话框。我也收到以下错误

在此处输入图像描述

并且应用程序冻结。

4

1 回答 1

0

你可以等待一个条件

class ProgressBarThread implements IRunnableWithProgress
{
    private static final int TOTAL_TIME = 5000;
    private static final int INCREMENT = 1000;

    private Condition done = null;

    public ProgressBarThread(Condition done)
    {
        this.done = done;
    }

    public void run(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException
    {
        monitor.beginTask("Sending Plot", IProgressMonitor.UNKNOWN);
        for (int total = 0; total < TOTAL_TIME ; total += INCREMENT)
        {
            if (done.await(INCREMENT, TimeUnit.MILLISECONDS))
            {
                break;
            }
            monitor.worked(INCREMENT);
            if (total == TOTAL_TIME / 2) monitor.subTask("Please be patient... Operation should finish soon.");
        }
        monitor.done();
    }
}

条件由主进程创建:

AplotPlotterDialog w = null;

Condition done = null;

public MyOperationListener(AplotPlotterDialog win)
{
    w = win;
    Lock lock = new ReentrantLock();
    done = lock.newCondition();
} 

public void startProgressBar()
{
    try
    {
        new ProgressMonitorDialog(getShell()).run(true, false,
        new ProgressBarThread(done));
    } 
    catch (InvocationTargetException e)
    {
        MessageDialog.openError(getShell(), "Error", e.getMessage());
    }  
    catch (InterruptedException e)
    {
        MessageDialog.openInformation(getShell(), "Cancelled", e.getMessage());
    }
}

public void endOperation()
{
    try
    {
        endOperationImpl();
    }
    finally
    {
        Display.getDefault().asyncExec(new Runnable() {
        public void run() {
        w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
        w.recursiveSetEnabled(getShell(), true);
        w.getShell().setEnabled(!getShell().getEnabled());
        done.signal();
        w.close();
    }
    });
    }
}

在操作进行期间,行为将与您当前的版本相同。

但是当主进程完成时,它将通过发出条件信号通知进度条线程,该线程将立即退出循环。

于 2013-01-11T23:00:06.057 回答