在 Eclipse 4 中正确使用 ProgressMonitorDialog
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12986912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using ProgressMonitorDialog in Eclipse 4 properly
提问by Cratylus
I have a set of APIs that do file operations e.g. saveToFile(CustomObject objectToSave);
我有一组执行文件操作的 API,例如 saveToFile(CustomObject objectToSave);
Since a file operation could be lengthy I decided that some indication should be shown to the user e.g. progress bar.
由于文件操作可能很长,我决定应该向用户显示一些指示,例如进度条。
I read about a ProgressMonitorDialog
and so I tried it, but it doesn't exactly work as I need (or better I don't know how to use it properly).
我阅读了 a ProgressMonitorDialog
,所以我尝试了它,但它并没有完全按照我的需要工作(或者更好的是我不知道如何正确使用它)。
Currently I do:
目前我这样做:
ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(theShell);
try {
progressDialog.run(false, true, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.beginTask("Saving your data", 100);
try {
Utils.saveToFile(objectToSave);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
monitor.done();
}
});
This code shows a progress dialog very fast and ends, but the problem is that on a slower PC this would stack until the Utils.saveToFile
returned, while I have no idea how to indicate intermediate process until the save is completed.
这段代码显示了一个非常快的进度对话框并结束,但问题是在较慢的 PC 上这将堆叠直到Utils.saveToFile
返回,而我不知道如何指示中间过程直到保存完成。
I found a threadmentioning about IProgressMonitor.UNKNOWN
but it does not say about what happens in monitor
during the performRead(_fileName, monitor);
我发现一个线程提到了IProgressMonitor.UNKNOWN
但它没有说明monitor
在performRead(_fileName, monitor);
How would I solve this?
我将如何解决这个问题?
回答by Baz
ProgressMonitorDialog
is a tricky piece of code. I guess the part you are missing is IProgressMonitor#worked(int)
which will "grow" the progress bar. Below is a code example that should clarify how to use it:
ProgressMonitorDialog
是一段棘手的代码。我想您缺少的部分是IProgressMonitor#worked(int)
将“增长”进度条的部分。下面是一个代码示例,应该阐明如何使用它:
public class Progress {
public static void main(String[] args)
{
// Create your new ProgressMonitorDialog with a IRunnableWithProgress
try {
// 10 is the workload, so in your case the number of files to copy
IRunnableWithProgress op = new YourThread(10);
new ProgressMonitorDialog(new Shell()).run(true, true, op);
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
private static class YourThread implements IRunnableWithProgress
{
private int workload;
public YourThread(int workload)
{
this.workload = workload;
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
// Tell the user what you are doing
monitor.beginTask("Copying files", workload);
// Do your work
for(int i = 0; i < workload; i++)
{
// Optionally add subtasks
monitor.subTask("Copying file " + (i+1) + " of "+ workload + "...");
Thread.sleep(2000);
// Tell the monitor that you successfully finished one item of "workload"-many
monitor.worked(1);
// Check if the user pressed "cancel"
if(monitor.isCanceled())
{
monitor.done();
return;
}
}
// You are done
monitor.done();
}
}
}
It will look something like this:
它看起来像这样:
For your special case of using Utils.saveToFile
you could hand the IProgressMonitor
over to this method and call the worked()
method from there.
对于您使用的特殊情况,Utils.saveToFile
您可以将其IProgressMonitor
移交给此方法并worked()
从那里调用该方法。