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, which 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 Job
of HelloHandler
and add a condition in the for loop that breaks out if the number of execution reaches 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
instance.} else if (i == 12) { checkDozen(new SubProgressMonitor(monitor, 100)); continue; }
continue
statement is used here to avoid calling monitor.worked(100)
.The checkDozen()
method took an IProgressMonitor
instance, 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
instance was passed in. 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.