0

我想在我的应用程序中实现一个进度监视器对话框。该功能是将大文件/文件夹从一个位置复制到 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");
  }
}
4

1 回答 1

0

Something like this could be used (it is just rough example, which can be improved in many ways):

        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
                }
            }
        }
于 2013-10-09T07:52:31.300 回答