我想在我的应用程序中实现一个进度监视器对话框。其功能是在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");
}
}发布于 2013-10-09 15:52:31
可以使用这样的东西(这只是一个粗略的例子,可以在许多方面进行改进):
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
}
}
}https://stackoverflow.com/questions/19264001
复制相似问题