views:

545

answers:

3

I have a job (class which extends org.eclipse.core.runtime.jobs.Job) running inside Eclipse. The job gets an IProgressMonitor and I'm using it to report progress, that's all good.

Here's my problem: during the processing, I sometimes find out that there's more work than I anticipated. Sometimes even double. However, once I set the total number of ticks in the progress monitor, there's no way of changing this value.

Any ideas on how to overcome this problem?

+1  A: 

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.

Robert Munteanu
I'm familiar with it and that's no good. It only helps if you know in advance that you will need more work. I don't otherwise, I could just give a higher tick in the first place. If there is no more work, the first 30% will be crawling and the next will just fly. That's not a good user experience.
zvikico
It does help if you put in the most consuming amount of work first.
Robert Munteanu
Again, which means you need to know there will be more work and that it will be less than the initial work.
zvikico
As I said in my response, I think this is unavoidable if it is not known at the start of the process how much work is required. The downvote seems harsh
Rich Seller
I'm sorry, but this answer is irrelevant to my question. Your answer says it is unavoidable and that's something else.
zvikico
A: 

There's an eclipse.org article on the use of progress monitors that may help you in general. AFAIK there is no way to resize the number of ticks in the monitor, so unless you do an initial pass to guess the relative size of the tasks and assign the ticks to each part you will get jumps.

You could allocate the first 10% to determining the size of the job, often though you can't do that until you're done, so you just end up shifting the sticking point on the progress bar.

Rich Seller
I found at least two workarounds so far, none is perfect, but both working: 1. Spawn a new job with a new monitor.2. slow down the progress of my current job if I recognize that more work is coming.
zvikico
A: 

Sounds like a "regress monitor" to me :-)

Let's say you're displaying a 50% progress, and you find out that you're actually only at 25%, what are you going to do? Go back?

Maybe you can implement your own IProgressMonitor to do that, but I'm not sure of the added value for your user

Alexandre Pauzies