When performing a set of operations, subtasks can give the user additional details about the state of the operation. A subtask is merely a named message that is displayed along with the task name in the Progress view.
monitor.subTask
during the operation to give feedback:for (int i=0; i<50 && !monitor.isCanceled(); i++) { if (i == 10) { monitor.subTask("Doing something"); } else if (i == 25) { monitor.subTask("Doing something else"); } else if (i == 40) { monitor.subTask("Nearly there"); } Thread.sleep(100); monitor.worked(100); }
checkDozen
, to the anonymous Job
class inside the HelloHandler
class, and add a condition in the for
loop that breaks out on reaching 12
:protected IStatus run(IProgressMonitor monitor) { ... } else if (i == 12) { checkDozen(monitor); } ... } private void checkDozen(IProgressMonitor monitor) { try { monitor.beginTask("Checking a dozen", 12); for (int i = 0; i < 12; i++) { Thread.sleep(10); monitor.worked(1); } } catch (InterruptedException e) { } finally { monitor.done(); } }
IProgressMonitor
instance and pass that into the method call using a SubProgressMonitor
:} else if (i == 12) { // NB SubProgressMonitor is deprecated // Will be replaced with SubMonitor next checkDozen(new SubProgressMonitor(monitor, 100)); continue; }
continue
is used here to avoid calling monitor.worked(100)
below.The checkDozen
method took an IProgressMonitor
argument, and simulated a set of different tasks (with different units of work). Passing the same monitor instance causes problems as the work gets missed between the two.
To fix this behavior, a SubProgressMonitor
was used. Because the SubProgressMonitor
got 100 units of work from its parent, when the done
method was called on the SubProgressMonitor
the parent saw the completion of the 100 units of work.
Importantly, this also allows the child to use a completely different scale of work units and be completely decoupled from the parent's use of work units.
3.144.113.30