Time for action – using subtasks and sub-progress 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 that is displayed along with the task name in the Progress view.

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

What just happened?

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.

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

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