Time for action – dealing with cancellation

Sometimes the user will change their mind; they may have selected the wrong option, or something more important may have come up. The progress monitor allows for two-way communication; the user can signify when they want to cancel as well. There is a method, isCancelled, which returns true if the user has signified in some way that they want the job to finish early.

Periodically checking this during the operation of the Job allows the user to cancel a long-running job before it reaches the end.

  1. Modify the for loop in the HelloHandler to check on each iteration whether the monitor is cancelled:
    for (int i = 0; i < 50 && !monitor.isCanceled(); i++) { ... } if (!monitor.isCancelled()) { display.asyncExec(() ...

Get Eclipse Plug-in Development Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.