我的RCp应用程序中有一个耗时的工作,我在其中读出一个大数据库并将其保存在一个文件中。我在一个单独的线程中运行此作业,以便我的应用程序的UI不被阻止,但我不知道如何为此任务添加进度条
我的代码:
public class MirrorFeatureModel extends Job {
protected class MutexRule implements ISchedulingRule {
public boolean isConflicting(ISchedulingRule rule) {
return rule == this;
}
public boolean contains(ISchedulingRule rule) {
return rule == this;
}
}
private String source;
private String template;
private String target;
public MirrorFeatureModel(String name) {
super(name);
}
public MirrorFeatureModel(String source, String template, String target){
super("Mirroring SWA Model");
this.source = source;
this.template = template;
this.target = target;
}
public void run() {
this.schedule();
}
@Override
protected IStatus run(IProgressMonitor monitor) {
ILog logView = Activator.getDefault().getLog();
String connectionString = this.source;
String emptyEAP = this.template;
String target = this.target;
try{
monitor.beginTask("Copying swa model to local", 1);
new Mirror(connectionString, emptyEAP,target).run();
monitor.worked(1);
}catch(final Exception e) {
logView.log(new Status(Status.ERROR, null , "Failed to create local SAM instance", e));
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(PlatformUI.getWorkbench().getDisplay().getActiveShell(), "Failed to create local SAM instance", e.getMessage());
}
});
return Status.CANCEL_STATUS;
}
return Status.OK_STATUS;
}
}
Mirror.run()方法
public void run(IProgressMonitor monitor) {
try{
if (monitor != null)
monitor.subTask("Creating directory for eap file");
File tempTarget=File.createTempFile("eap-mirror", "eap");
if (monitor != null)
monitor.worked(1);
try {
if (monitor != null)
monitor.subTask("Establising connection");
this.source=DriverManager.getConnection(com.intel.imc.swa.easql.EaDbStringParser.eaDbStringToJdbc(sourceString));
this.source.setReadOnly(true);
if(monitor != null)
monitor.worked(1);
FileUtils.copyFile(new File(templateFileString), tempTarget);
if (monitor != null)
monitor.subTask("Opening database");
this.target=Database.open(tempTarget,false,false);
if(monitor != null)
monitor.worked(1);
if (monitor != null)
monitor.subTask("Mirroring tables");
Collection<String> tables=selectTables(source);
long time=System.currentTimeMillis();
for (String tableName : tables) {
long tTime=System.currentTimeMillis();
Table table=target.getTable(tableName);
System.out.print("Mirroring table "+tableName+"...");
table.setOverrideAutonumber(true);
copyTable(table, source, target);
System.out.println(" took "+(System.currentTimeMillis()-tTime));
}
System.out.println("Done. Overall time: "+(System.currentTimeMillis()-time));
if(monitor != null)
monitor.worked(1);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
如何在此课程中添加一个进度条,向我显示此作业的进度
谢谢
答案 0 :(得分:1)
通常,作业会在进度视图和主窗口底部的状态行中显示进度指示器。
如果您在setUser(true)
之前致电schedule()
,那么如果作业运行超过几秒钟,作业将显示弹出进度对话框。
要显示工作进度,您必须在beginTask
电话
monitor.beginTask("...", total work);
然后每次完成部分工作时都必须致电monitor.worked(xxx)
。