Time for action – using subtasks and subprogress monitors

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.

  1. Add 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);
    }
  2. Run the Eclipse instance, and look at the Progress view. The subtask should be shown underneath the status bar:

    Time for action – using subtasks and subprogress monitors

  3. When calling another method with a progress monitor, if the monitor is passed as it is, it can have undesirable effects. Add a new method, 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();
      }
    }
  4. Run the Eclipse instance, select the Hello menu and open the Progress view and the progress status completely disappears after it reaches that point:
    Time for action – using subtasks and subprogress monitors
  5. To solve this problem, create another IProgressMonitor instance and pass that into the method call using a SubProgressMonitor instance.
    } else if (i == 12) {
      checkDozen(new SubProgressMonitor(monitor, 100));
      continue;
    }
  6. Now, run the action, and the progress will update as expected. Note, that the continue statement is used here to avoid calling monitor.worked(100).

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset