Take a look at SubMonitor.
void doSomething(IProgressMonitor monitor) {
// Convert the given monitor into a progress instance
SubMonitor progress = SubMonitor.convert(monitor, 100);
// Use 30% of the progress to do some work
doSomeWork(progress.newChild(30));
// Advance the monitor by another 30%
progress.worked(30);
// Use the remaining 40% of the progress to do some more work
doSomeWork(progress.newChild(40));
}
Technical details aside, this is how I would do it:
- your usual work is 100;
- set an initial work of 200;
- increment work as needed when you progress, assuming that the total work to do is 100;
- when the work is done, signal its completion.
This has the following effects:
- on a regular work item, which takes 100 units, it ends up completing very fast after 50% progress;
- on a long work item, it ends up with a nice steady progress.
This is better by completing faster than the user expects, and not seeming to be stuck for a long amount for time.
For bonus points, if/when the potentially long sub-task is detected to have complete fast enough, still increment the progress by the large amount. This avoids the jump from 50% to complete.