首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >进度监视器对话框计算总时间

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

Stack Overflow用户
提问于 2013-10-09 13:42:27
回答 1查看 354关注 0票数 0

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

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

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

以下是示例代码:

代码语言:javascript
复制
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");
  }
}
EN

回答 1

Stack Overflow用户

发布于 2013-10-09 15:52:31

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

代码语言:javascript
复制
        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
                }
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19264001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档