在Eclipse 4中正确使用ProgressMonitorDialog [英] Using ProgressMonitorDialog in Eclipse 4 properly
问题描述
saveToFile(CustomObject objectToSave);
由于文件操作可能很冗长,我决定显示一些指示用户例如进度条。
我读了一个 ProgressMonitorDialog
,所以我尝试了,但它并没有完全正常工作,因为我需要或者更好,我不知道如何正确使用它)。
目前我做:
ProgressMonitorDialog progressDialog = new ProgressMonitorDialog );
尝试{
progressDialog.run(false,true,new IRNableWithProgress(){
@Override
public void run(IProgressMonitor monitor)throws InvocationTargetException,InterruptedException {
monitor.beginTask(保存数据,100);
try {
Utils.saveToFile(objectToSave);
} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace();
}
monitor.done();
}
});
此代码显示进度对话框非常快速并结束,但问题是在较慢的PC上将堆栈,直到 Utils.saveToFile
返回,而我不知道如何指示中间进程,直到保存完成。
我发现一个线程提到关于 IProgressMonitor.UNKNOWN
但不会说明在 monitor
在 performRead(_fileName,monitor);
我该如何解决?
ProgressMonitorDialog
是一个棘手的代码。我猜你缺少的部分是 IProgressMonitor#working(int)
,这将增长进度条。以下是一个应该澄清如何使用它的代码示例:
public class Progress {
public static void main String [] args)
{
//使用IRunnableWithProgress创建新的ProgressMonitorDialog
try {
// 10是工作负载,所以在你的情况下要复制的文件数
IRunnableWithProgress op = new YourThread(10);
new ProgressMonitorDialog(new Shell())。run(true,true,op);
} catch(InvocationTargetException ex){
ex.printStackTrace();
} catch(InterruptedException ex){
ex.printStackTrace();
}
}
私有静态类YourThread实现IRunnableWithProgress
{
private int工作负载;
public YourThread(int工作负载)
{
this.workload =工作负载;
}
@Override
public void run(IProgressMonitor monitor)throws InvocationTargetException,InterruptedException
{
//告诉用户您正在做什么
monitor.beginTask(复制文件,工作负载);
//为您的工作
(int i = 0; i<工作负载; i ++)
{
//可选添加子任务
monitor .subTask(复制文件+(i + 1)++工作负载+...);
Thread.sleep(2000);
//告诉监视器你成功完成了一个工作负载项目-many
monitor.worked(1);
//检查用户是否按取消
if(monitor.isCanceled())
{
monitor.done();
return;
}
}
//你完成
monitor.done();
}
}
}
看起来像这样:
对于使用 Utils.saveToFile
的特殊情况,您可以将 IProgressMonitor
到这个方法,并从那里调用 working()
方法。
I have a set of APIs that do file operations e.g. 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).
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.
I found a thread mentioning about IProgressMonitor.UNKNOWN
but it does not say about what happens in monitor
during the performRead(_fileName, monitor);
How would I solve this?
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:
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.
这篇关于在Eclipse 4中正确使用ProgressMonitorDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!