What is the proper way to use progress monitors for an Eclipse Job that schedules and joins on another Eclipse job? I'd like the subtask to be associated with the main task.
Ideally, I'd like to be able to tell the subtask to run with a SubProgressMonitor
created from the main monitor, however I don't see a way to do this. I've looked at the Job.setProgressGroup(..., ...)
method, but the documentation indicates that the group should be created with IJobManager.createProgressGroup()
.
Here's a code snippet for context:
@Override
protected IStatus run(final IProgressMonitor monitor) {
try {
monitor.beginTask("My Job A", 100);
MyJobB subtask = new MyJobB();
// how how should subtask's progress be tracked?
subtask.schedule();
subtask.join();
return Status.OK_STATUS;
} catch (Exception ex) {
// return error status
} finally {
monitor.done();
}
}
To answer the original question - you can do it this way:
public class MyJobA extends Job {
private IProgressMonitor pGroup = Job.getJobManager().createProgressGroup();
public MyJobA() {
super("My Job A");
setProgressGroup(pGroup, 50);
}
@Override
protected IStatus run(IProgressMonitor monitor) {
MyJobB subtask = new MyJobB();
subtask.setProgressGroup(pGroup, 50);
pGroup.beginTask("My Jobs", 100);
try {
monitor.beginTask("My Job A", 10);
try {
// do work
monitor.worked(...);
} finally {
monitor.done();
}
subtask.schedule();
subtask.join();
} finally {
pGroup.done();
}
return Status.OK_STATUS;
}
}
It's a bit more elegant, if you create the progress group externally:
IProgressMonitor group = Job.getJobManager().createProgressGroup();
try {
group.beginTask("My Tasks", 200);
MyJobB b = new MyJobB();
b.setProgressGroup(group, 100);
b.schedule();
MyJobC c = new MyJobC();
c.setProgressGroup(group, 100);
c.schedule();
b.join();
c.join();
} finally {
group.done();
}
Please note that a Job that is not scheduled yet will be displayed as "finished":
If possible I would consider merging the two Jobs and using a SubMonitor.