Time for action – reporting progress

Normally when a Job is running, it is necessary to periodically update the user to let them know the state of the progress. By default, if a Job provides no information, a generic busy indicator is shown. When a Job is executed, it is passed an IProgressMonitor, which can be used to notify the user of the progress (and provide a way to cancel the operation). A progress monitor has a number of tasks, each of which has a total unit of work that it can do. For jobs that don't have a known amount of work, UNKNOWN can be specified and it will display in a generic busy indicator.

  1. Open the HelloHandler and go to the execute method. In the inner run method, add a beginTask at the beginning, and a worked in the loop after each second's sleep, for five iterations. The code will look like:
    protected IStatus run(IProgressMonitor monitor) {
      try {
        monitor.beginTask("Preparing", 5000);
        for (int i = 0; i < 5; i++) {
          Thread.sleep(1000);
          monitor.worked(1000);
        }
      } catch (InterruptedException e) {
      } finally {
        monitor.done();
      }
      display.asyncExec(() -> {
        MessageDialog.openInformation(null, "Hello", "World");
      });
      return Status.OK_STATUS;
    }
  2. Run the target Eclipse instance, and open the Progress view by navigating to Window | Show View | Other | General | Progress. Now invoke the Help | Hello menu; the Progress view should show the progress as the sleep occurs.
  3. To make the reporting more accurate, report the status more frequently:
    for (int i = 0; i < 50; i++) {
      Thread.sleep(100);
      monitor.worked(100);
    }
  4. Run the target Eclipse instance again. Now when the job is run via the Help | Hello menu, the status will be updated in the Progress view more frequently.

What just happened?

When running a Job, the progress monitor can be used to indicate how much work has been done. It must start with a beginTask—this gives both the total number of work units as well as a textual name that can be used to identify what's happening.

If the amount of work is unknown, use IProgressMonitor.UNKNOWN.

The unit scale doesn't really matter; it could have been 50 or 50,000. As long as the total number of work units adds up and they're appropriately used, it will still give the user a good idea of the operation.

Don't just report based on the number of lines (or tasks). If there are four work items but the fifth one takes as long as the previous four, then the amount of work reported needs be balanced; for example, provide a total of 8 units, with 1 unit for each of the first four and then the remaining 4 for the fifth item.

Finally, done was called on the progress monitor. This signifies that the Job has been completed, and can be removed from any views that are reporting the status. This is wrapped inside a finally block to ensure that the monitor is completed even if the job finishes abnormally (for example, if an exception occurs).

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

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