Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "FAQ How do I use progress monitors?"

m
(See Also:)
Line 59: Line 59:
 
*[[FAQ Why should I use the new progress service?]]
 
*[[FAQ Why should I use the new progress service?]]
  
<hr><font size=-2>This FAQ was originally published in [http://www.eclipsefaq.org Official Eclipse 3.0 FAQs]. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the [http://www.eclipse.org/legal/epl-v10.html Eclipse Public License v1.0].</font>
+
{{Template:FAQ_Tagline}}

Revision as of 14:01, 30 May 2013

A progress monitor is a callback interface that allows a long-running task to report progress and respond to cancellation. Typically, a UI component will create a monitor instance and pass it to a low-level component that does not know or care about the UI. Thus, an IProgressMonitor is an abstraction that allows for decoupling of UI and non-UI components.

Each monitor instance has a strictly defined lifecycle. The first method that must be called is beginTask, which specifies a description of the operation and the number of units of work that it will take. This work value doesn&#146;t need to be very precise; your goal here is to give the user a rough estimate of how long it will take. If you have no way of estimating the amount of work, you can pass a work value of IProgressMonitor.UNKNOWN, which will result in a continuously animated progress monitor that does not give any useful information to the user.

After beginTask, you should call subTask and worked periodically as the task progresses. The sum of the values passed to the worked method must equal the total work passed to beginTask. The subTask messages can be sent as often as you like, as they provide more details about what part of the task is currently executing. Again, you don&#146;t need to be precise here. Simply give the user a rough idea of what is going on.

Finally, you must call done on the monitor. One consequence of calling done is that any unused portion of the progress bar will be filled up. If your code is part of a larger operation, failing to call done will mean that the portion of the progress bar allotted to your part of the operation will not be filled. To ensure done gets called, you should place it in a finally block at the very end of your operation.

Here is a complete example of a long-running operation reporting progress:

   try {
      monitor.beginTask("Performing decathlon: ", 10);
      monitor.subTask("hammer throw");
      //perform the hammer throw
      monitor.worked(1);
      //... repeat for remaining nine events
   } finally {
      monitor.done();
   }

The monitor can also be used to respond to cancellation requests. When the user requests cancellation, the method isCanceled will return true. Your long-running operation should check this value occasionally and abort if a cancellation has occurred. A common method of quickly aborting a long-running operation is to throw OperationCanceledException.

See Also:


This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.