Jump to: navigation, search

Difference between revisions of "FAQ How do I switch from using a Progress dialog to the Progress view?"

 
m
Line 1: Line 1:
''
 
 
 
 
 
 
If you have an existing plug-in that uses a <tt>ProgressMonitorDialog</tt>,  
 
If you have an existing plug-in that uses a <tt>ProgressMonitorDialog</tt>,  
 
you can easily switch to using the Progress view by rewriting your operation as
 
you can easily switch to using the Progress view by rewriting your operation as
 
a <tt>org.eclipse.core.runtime.Job</tt>. Assume that your original code looks like this:
 
a <tt>org.eclipse.core.runtime.Job</tt>. Assume that your original code looks like this:
 +
 
<pre>
 
<pre>
 
   IRunnableWithProgress op = new IRunnableWithProgress() {
 
   IRunnableWithProgress op = new IRunnableWithProgress() {
Line 18: Line 14:
 
   new ProgressMonitorDialog(shell).run(true, true, op);
 
   new ProgressMonitorDialog(shell).run(true, true, op);
 
</pre>
 
</pre>
 +
 
The equivalent code using <tt>org.eclipse.core.runtime.Job</tt> would look like this:
 
The equivalent code using <tt>org.eclipse.core.runtime.Job</tt> would look like this:
 +
 
<pre>
 
<pre>
 
   class DecathlonJob extends Job {
 
   class DecathlonJob extends Job {
Line 31: Line 29:
 
   new DecathlonJob().schedule();
 
   new DecathlonJob().schedule();
 
</pre>
 
</pre>
 
  
 
Both use an <tt>IProgressMonitor</tt> to report progress to the user. The major  
 
Both use an <tt>IProgressMonitor</tt> to report progress to the user. The major  
Line 38: Line 35:
 
<tt>Job</tt> is used, it will run in the background, and the user can continue  
 
<tt>Job</tt> is used, it will run in the background, and the user can continue  
 
working on something else.
 
working on something else.
 
  
 
Although the changes required here appear to be simply cosmetic, keep in mind
 
Although the changes required here appear to be simply cosmetic, keep in mind
Line 49: Line 45:
 
it can lead to deadlock.  Read up on your concurrent  
 
it can lead to deadlock.  Read up on your concurrent  
 
programming before you venture down this path.
 
programming before you venture down this path.
 
 
  
 
== See Also: ==
 
== See Also: ==
 
+
*[[FAQ_How_do_I_use_progress_monitors%3F]]
[[FAQ_How_do_I_use_progress_monitors%3F]]
+
*[[FAQ_Does_the_platform_have_support_for_concurrency%3F]]
 
+
*[[FAQ_Actions%2C_commands%2C_operations%2C_jobs%3A_What_does_it_all_mean%3F]]
[[FAQ_Does_the_platform_have_support_for_concurrency%3F]]
+
 
+
[[FAQ_Actions%2C_commands%2C_operations%2C_jobs%3A_What_does_it_all_mean%3F]]
+
  
 
<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>
 
<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>

Revision as of 22:16, 29 May 2006

If you have an existing plug-in that uses a ProgressMonitorDialog, you can easily switch to using the Progress view by rewriting your operation as a org.eclipse.core.runtime.Job. Assume that your original code looks like this:

   IRunnableWithProgress op = new IRunnableWithProgress() {
      public void run(IProgressMonitor monitor) {
         runDecathlon(monitor);
      }
   };
   IWorkbench wb = PlatformUI.getWorkbench();
   IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
   Shell shell = win != null ? win.getShell() : null;
   new ProgressMonitorDialog(shell).run(true, true, op);

The equivalent code using org.eclipse.core.runtime.Job would look like this:

   class DecathlonJob extends Job {
      public DecathlonJob() {
         super("Athens decathlon 2004");
      }
      public IStatus run(IProgressMonitor monitor) {
         runDecathlon(monitor);
         return Status.OK_STATUS;
      }
   };
   new DecathlonJob().schedule();

Both use an IProgressMonitor to report progress to the user. The major difference is that the ProgressMonitorDialog is a modal dialog and blocks access to the entire UI during the execution of runDecathlon. When a Job is used, it will run in the background, and the user can continue working on something else.

Although the changes required here appear to be simply cosmetic, keep in mind that there are subtle implications to running your operation in the background. Foremost, you must ensure that your operation code is thread safe in case two copies of the operation start running simultaneously. You also need to think about whether your background operation will be in contention with other ongoing processes for exclusive resources, such as Java object monitors. Contention between threads can block the user interface; worse, it can lead to deadlock. Read up on your concurrent programming before you venture down this path.

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.