进度监视器对话框计算总时间

时间:2013-10-09 05:42:27

标签: eclipse eclipse-plugin osgi eclipse-rcp osgi-bundle

我想在我的应用程序中实现进度监视器对话框。功能是将大文件/文件夹从一个位置复制到 另一个位置winthin Windows。如果我们在窗口内复制和粘贴,可能需要大约7-10分钟。 当我们通过eclipse rcp进度监视器对话框实现时,我们如何计算完成任务的总时间?因为对于较小的文件,它可能花费的时间非常少,而对于较大的文件则需要很长的时间。那么TOTAL_TIME = 10000.对她的硬c有什么好处呢? 一旦工作完成,我们可以说它需要大约7或8分钟。这是我在阅读以下代码时遇到的困惑。

我会根据文件大小算法复制。

我得到了一个示例,总时间被称为TOTAL_TIME = 10000.

以下是示例代码:

public void run(IProgressMonitor monitor) throws InvocationTargetException,
      InterruptedException {
    monitor.beginTask("Running long running operation",
        indeterminate ? IProgressMonitor.UNKNOWN : TOTAL_TIME);
    for (int total = 0; total < TOTAL_TIME && !monitor.isCanceled(); total += INCREMENT) {
      Thread.sleep(INCREMENT);
      monitor.worked(INCREMENT);
      if (total == TOTAL_TIME / 2) monitor.subTask("Doing second half");
    }
    monitor.done();
    if (monitor.isCanceled())
        throw new InterruptedException("The long running operation was cancelled");
  }
}

1 个答案:

答案 0 :(得分:0)

可以使用这样的东西(这只是粗略的例子,可以通过多种方式进行改进):

        FileChannel src = null;
        FileChannel dest = null;
        try {
            src = new FileInputStream(file1).getChannel();
            dest = new FileOutputStream(file2).getChannel();

            int offset = 1024;
            long totalSize = src.size();
            long position = 0;

            int numberOfIterations = (int) (totalSize / offset);
            int currentIteration = 0;

            monitor.beginTask("Running long running operation", numberOfIterations);

            while (!monitor.isCanceled()) {
                long start = System.currentTimeMillis();
                dest.transferFrom(src, position, position + offset);
                long end = System.currentTimeMillis();
                monitor.worked(currentIteration++);
                long timeElapsedPerOneIteration = (end - start) / 1000;
                monitor.setTaskName("Running long running operation. Time left: "
                    + ((timeElapsedPerOneIteration * numberOfIterations) - timeElapsedPerOneIteration * currentIteration)
                    + " seconds.");
                position += offset;
                if (position >= totalSize) {
                    monitor.done();
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            // hanlde
        } catch (IOException e) {
            // hanlde
        } finally {
            if (src != null) {
                try {
                    src.close();
                } catch (IOException e) {
                    // hanlde
                }
            }
            if (dest != null) {
                try {
                    dest.close();
                } catch (IOException e) {
                    // hanlde
                }
            }
        }