【问题标题】:progress monitor dialog calculating total time进度监视器对话框计算总时间
【发布时间】:2013-10-16 08:06:53
【问题描述】:

我想在我的应用程序中实现一个进度监视器对话框。功能是将大文件/文件夹从一个位置复制到 Windows 内的另一个位置。如果我们在 Windows 中进行复制和粘贴,可能需要大约 7-10 分钟。 当我们通过 eclipse rcp 进度监视器对话框实现时,我们如何计算完成任务的总时间?因为对于较小的文件可能需要更少的时间,而对于较大的文件则需要大量时间。那么硬编码TOTAL_TIME = 10000. 比她有什么优势呢? 工作完成后,我们可以说大约需要 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");
  }
}

【问题讨论】:

  • 如果您可以提供有关如何复制文件的更多详细信息,那就更容易了。您可以尝试使用与文件大小相关的算法并复制内容,控制复制的字节数。在这种情况下,也可以计算整个操作的时间。
  • @Alexander Gavrilov 我将使用文件大小算法

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


【解决方案1】:

可以使用这样的东西(这只是一个粗略的例子,可以在很多方面进行改进):

        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
                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-07
    • 2015-07-21
    • 1970-01-01
    • 2012-12-16
    • 2015-04-16
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多