SWT - 调整我的 ProgressMonitorDialog [英] SWT - Tweaking my ProgressMonitorDialog

查看:44
本文介绍了SWT - 调整我的 ProgressMonitorDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可用的 ProgressMonitorDialog,但我想确保我正确设置了它.

I have a working ProgressMonitorDialog, but I want to make sure that I am setting it up correctly.

首先是代码:

创建对话框的方法

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

类文件

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

  public ProgressBarThread() {

  }

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

  }
}

调用ProgressBar并运行Pdf文件创建操作的方法

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) {

     public void endOperationImpl() {
        java.io.File zipFile = null;
        try {               
           AplotSaveResultsParser.SaveResult saveResults = saveOp.getSaveResults();
           if (saveResults != null) {
           ETC.....   ETC......  

问题:

  1. 作为 ProgressMonitorDialog 是一个 GUI,它需要在一个Display.getDefault().asyncExec?

  1. Being the ProgressMonitorDialog is a GUI, it needs to be executed in a Display.getDefault().asyncExec?

如果 ProgressMonitorDialog 在单独的线程中运行,它如何知道在操作完成时关闭?

If the ProgressMonitorDialog is running in a separate thread, how does it know to close when the operation is finsihed?

进度条和操作有关系吗?

Is there any relationship between the progressbar and the operation?

我认为 ProgressBarThread 类中的 for 循环基本上是保持监视器打开的计时器是正确的吗?

I am correct in assuming that the for loop in the ProgressBarThread class is basically the timer that keeps the monitor open?

有没有办法提高ProgressMonitorDialog的指示器的速度,也可以去掉取消按钮吗?

Is there a way to increase the speed of the ProgressMonitorDialog's indicator, also can you remove the cancel button?

这就是我认为目前正在发生的事情.

This is what I am thinking is happening currently.

  1. 我正在启动 PDF 操作侦听器之前启动进度条参见上面的 startSavePdfOperation()

进度条以未知方式运行,但使用 for 循环使进度条对话框保持打开状态,同时操作在后台线程上运行.
参见上面的 ProgressBarThread 类

The progress bar is running as unknown, but using a for loop to keep the progress bar dialog open, while the operation is running on a thread in the background.
See Class ProgressBarThread above

PDF 操作完成后,侦听器操作类将关闭基本 GUI 对话框.

When the PDF operation completes the listener operation class closes the base GUI dialog.

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();
       }
    });
 }

}

我不确定 ProgressBarThread 监视器发生了什么情况?

I am not sure what is happening to the ProgressBarThread monitor?

这可能吗?

  1. PDF 操作开始时,ProgressMonitorDialog 打开并启动指示器.可以保持未知.

  1. When the PDF Operation starts, the ProgressMonitorDialog opens and starts the indicator. OK with keeping it unknown.

PDF 操作完成后,监视器关闭,然后是基础对话框

When the PDF Operation completes, the monitor closes, then the base Dialog

我只想打开进度条对话框,通知用户他们的请求正在后台运行.

I am just wanting to open progress bar dialog that will inform the user that their request is working in the background.

如上所述,上面的代码是有效的,但我担心让 Base GUI 关闭会破坏我的 Progress Thread 和 Monitor 不是一个好习惯.

As stated the above code works, but I am afraid by letting the closing of Base GUI, destroy my Progress Thread and Monitor is not good practice.

推荐答案

首先,在你的 ProgressBarThread#run() 中你应该使用 monitor.worked(1).您不需要设置总工作量,而是根据自上次调用以来完成的工作量增加它.

First of all, in your ProgressBarThread#run() you should use monitor.worked(1). You don't need to set the total worked but increment it by the amount of work done, since the last time it was called.

第一季度.是的,需要在显示线程中执行Q2.通常,需要完成的工作实际上是在传递给进度监视器对话框的 runnable 中执行的,以便您可以准确报告所取得的进度.因此,您的操作(如果是同步调用)应该从 ProgressBarThread#run() 内调用,以便您仅在一个文件处理完成时调用 monitor.worked(1).

Q1. Yes it needs to be executed in the display thread Q2. Normally the work that needs to be done is actually performed in the runnable that is passed to the progress monitor dialog so that you can accurately report the amount of progress made. So your operation (if it is a synchronous call) should be called from within ProgressBarThread#run() so that you call monitor.worked(1) only when one file processing is complete.

第三季度.你在运行什么样的操作,也许它已经支持显示进度条了,你只需要调用正确的API.它是 IUndoableOperation?

Q3. What kind of operation are you running, perhaps it already supports showing progress bar, and you just need to invoke the right API. Is it an IUndoableOperation?

第四季度.正如我所说,这种方法是有问题的,因为您永远无法准确报告进度并仅在操作完成时关闭对话框.但是,如果这是您唯一的选择,那么您可以将 monitor 引用保存在某处,以便其他线程可以访问它.一旦 monitor.done() 被调用,你的 ProgressBarThread#run() 应该返回,对话框将关闭.

Q4. As I said this approach is problematic because you can never accurately report the progress and close the dialog only when the operation is completed. But if this is the only choice you have, then you can just save the monitor reference somewhere so that it is accessible to the other thread. Once monitor.done() is called, your ProgressBarThread#run() should return, the dialog will close.

问题 5.您可以通过将正确的参数传递给 ProgressMonitorDialog#run(..) 来移除取消按钮:

Q5. You can remove the cancel button by passing the correct parameter to ProgressMonitorDialog#run(..):

new ProgressMonitorDialog(getShell()).run(true, false, new ProgressBarThread());

对于其余的问题,如果我知道您使用的是哪种操作(什么 API),我可以更好地回答.

For the rest of the questions I can better answer if I know what kind of operation (what API) you are using.

这篇关于SWT - 调整我的 ProgressMonitorDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆