将工作添加到Eclipse中的Job的进度监视器

时间:2023-01-12 15:54:34

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.

我有一个在Eclipse中运行的工作(扩展org.eclipse.core.runtime.jobs.Job的类)。这个工作得到了一个IProgressMonitor,我用它来报告进度,这一切都很好。

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?

关于如何克服这个问题的任何想法?

5 个解决方案

#1


Take a look at SubMonitor.

看看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;
  • 你平常的工作是100;

  • set an initial work of 200;
  • 设定初始工作200;

  • increment work as needed when you progress, assuming that the total work to do is 100;
  • 当你进步时,根据需要增加工作量,假设要做的总工作量是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;
  • 对于需要100个单位的常规工作项目,在50%的进度后最终完成得非常快;

  • 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.

对于奖励积分,如果/当检测到潜在的长子任务足够快时,仍然会增加大量的进度。这避免了从50%跳到完成。

#2


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.

有一篇关于使用进度监视器的eclipse.org文章可能对你有所帮助。 AFAIK无法调整监视器中的刻度数,因此除非您执行初始传递以猜测任务的相对大小并为每个部分分配刻度,否则您将获得跳转。

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.

您可以将前10%分配给确定作业的大小,通常在完成之前不能这样做,因此您最终会在进度条上移动粘贴点。

#3


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?

假设您显示50%的进度,而您发现实际上只有25%,您打算做什么?回去?

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

也许您可以实现自己的IProgressMonitor来做到这一点,但我不确定您的用户的附加价值

#4


I think you will find the problem is a bit more abstract than you think. The question you are asking is really "I have a job that I don't know how long it's going to take, when can I say that I am halfway done?" The answer: You can't. Progress bars are there to display progress as a percentage of the whole. If you don't know the total or the percentage then a progress bar is no fun.

我想你会发现问题比你想象的要抽象得多。你问的问题是“我有一份工作,我不知道要花多长时间,我什么时候可以说我已经完成了一半?”答案:你做不到。进度条用于显示进度占整体的百分比。如果你不知道总数或百分比,那么进度条就不好玩了。

#5


Convert your IProgressMonitor to a SubMonitor, then you can call SubMonitor.setWorkRemaining at any time to redistribute the remaining number of ticks.

将您的IProgressMonitor转换为SubMonitor,然后您可以随时调用SubMonitor.setWorkRemaining来重新分配剩余的滴答数。

The javadoc for SubMonitor has this example demonstrating how to report progress if you don't know the total number of ticks in advance:

SubMonitor的javadoc这个例子展示了如果你事先不知道滴答的总数,如何报告进度:

// This example demonstrates how to report logarithmic progress in 
// situations where the number of ticks cannot be easily computed in advance.

  void doSomething(IProgressMonitor monitor, LinkedListNode node) {
      SubMonitor progress = SubMonitor.convert(monitor);

      while (node != null) {
          // Regardless of the amount of progress reported so far,
          // use 0.01% of the space remaining in the monitor to process the next node.
          progress.setWorkRemaining(10000);

          doWorkOnElement(node, progress.newChild(1));

          node = node.next;
      }
  }

#1


Take a look at SubMonitor.

看看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;
  • 你平常的工作是100;

  • set an initial work of 200;
  • 设定初始工作200;

  • increment work as needed when you progress, assuming that the total work to do is 100;
  • 当你进步时,根据需要增加工作量,假设要做的总工作量是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;
  • 对于需要100个单位的常规工作项目,在50%的进度后最终完成得非常快;

  • 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.

对于奖励积分,如果/当检测到潜在的长子任务足够快时,仍然会增加大量的进度。这避免了从50%跳到完成。

#2


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.

有一篇关于使用进度监视器的eclipse.org文章可能对你有所帮助。 AFAIK无法调整监视器中的刻度数,因此除非您执行初始传递以猜测任务的相对大小并为每个部分分配刻度,否则您将获得跳转。

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.

您可以将前10%分配给确定作业的大小,通常在完成之前不能这样做,因此您最终会在进度条上移动粘贴点。

#3


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?

假设您显示50%的进度,而您发现实际上只有25%,您打算做什么?回去?

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

也许您可以实现自己的IProgressMonitor来做到这一点,但我不确定您的用户的附加价值

#4


I think you will find the problem is a bit more abstract than you think. The question you are asking is really "I have a job that I don't know how long it's going to take, when can I say that I am halfway done?" The answer: You can't. Progress bars are there to display progress as a percentage of the whole. If you don't know the total or the percentage then a progress bar is no fun.

我想你会发现问题比你想象的要抽象得多。你问的问题是“我有一份工作,我不知道要花多长时间,我什么时候可以说我已经完成了一半?”答案:你做不到。进度条用于显示进度占整体的百分比。如果你不知道总数或百分比,那么进度条就不好玩了。

#5


Convert your IProgressMonitor to a SubMonitor, then you can call SubMonitor.setWorkRemaining at any time to redistribute the remaining number of ticks.

将您的IProgressMonitor转换为SubMonitor,然后您可以随时调用SubMonitor.setWorkRemaining来重新分配剩余的滴答数。

The javadoc for SubMonitor has this example demonstrating how to report progress if you don't know the total number of ticks in advance:

SubMonitor的javadoc这个例子展示了如果你事先不知道滴答的总数,如何报告进度:

// This example demonstrates how to report logarithmic progress in 
// situations where the number of ticks cannot be easily computed in advance.

  void doSomething(IProgressMonitor monitor, LinkedListNode node) {
      SubMonitor progress = SubMonitor.convert(monitor);

      while (node != null) {
          // Regardless of the amount of progress reported so far,
          // use 0.01% of the space remaining in the monitor to process the next node.
          progress.setWorkRemaining(10000);

          doWorkOnElement(node, progress.newChild(1));

          node = node.next;
      }
  }