title: ProgressMonitorDialog - Watching active thread to update monitor
body: <p>In my GUI I have a PDF file creation operation.  The operation can take up to 10-15 seconds to complete. When I start the operation, I attach a listener to it.  The listener changes the cursor and disables the GUI, until the operation completes.<br>
  I would also like to add a progressbar, so the users will have a idea when it is going to complete.</p>
  
  <p>Created a method startProgressBar() and called it from the start of the operation method.<br>
  <em>See Below:</em></p>
  
  <pre><code>  private void startSavePdfOperation() {
        startProgressBar();
        saveOp = new AplotSaveOperation(appReg.getString("aplot.message.SAVETOPDF"), "PDF", session);
        saveOp.addOperationListener(new MyOperationListener(this) {
  </code></pre>
  
  <p><em>startProgressBar Method - See Below:</em></p>
  
  <pre><code> public void startProgressBar() {
       Shell shell = new Shell(getShell());
    shell.setSize(260, 120);
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds (20, 20, 200, 20);
    shell.open();
    final int maximum = bar.getMaximum();
    new Thread(new Runnable() {
       public void run() {
          for (final int[] i = new int[1]; i[0] &lt;= maximum; i[0]++) {
          try {Thread.sleep (100);} catch (Throwable th) {}
             if (Display.getDefault().isDisposed()) return;
             Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                if (bar.isDisposed ()) return;
                   bar.setSelection(i[0]);
                }
             });
          }
       }
    }).start();
  </code></pre>
  
  <p>The code above created the ProgressBar.  The issue is that the operation would end well before the progressbar indicator was close to ending.  </p>
  
  <p>Question:  Is this because in the method I am creating a new thread and the indicator is updating according to the new thread and not the operation thread?</p>
  
  <p>Question:  Is it possible to create a new thread that watches the GUI thread and updates the progressbar accordingly?</p>
  
  <p>Read a article suggesting using ProgressMonitorDialog with IRunnableWithProgress. </p>
  
  <p><em>Method startProgressBar using ProgressMonitorDialog - see below:</em></p>
  
  <pre><code> public void startProgressBar() {
    ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());    
    try {
       dialog.run(true, true, new IRunnableWithProgress(){
            public void run(IProgressMonitor monitor) {
                monitor.beginTask("Some nice progress message here ...", 100);
                ** getThread(); **
                monitor.done();
            }
        });
    }
    catch (InvocationTargetException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    }
    catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
    } 
  }
  
   public void getThread() {
     new Thread etc.. etc...
  
   }
  </code></pre>
  
  <p>It seems that it will have the same issues with threading and updating as the code above.</p>
  
  <p>Question:  So now I am thinking can I just add or update the ProgressBar to my existing Listener</p>
  
  <p><em>OperationListener Code - see below:</em></p>
  
  <pre><code> public abstract class MyOperationListener implements InterfaceAIFOperationListener {
    AplotCreatePDFDialog w = null;
  
    public MyOperationListener(AplotCreatePDFDialog win) {
       w = win;
    } 
    public void startOperation(String startMessage) {
       Display.getDefault().asyncExec(new Runnable() {
          public void run() {
             w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT));
             w.recursiveSetEnabled(getShell(), getShell().getEnabled());
             w.getShell().setEnabled(!getShell().getEnabled());
          }
       });
    } 
  
    public void endOperation() {
       try {
          endOperationImpl();
       }
       finally {
          Display.getDefault().asyncExec(new Runnable() {
             public void run() {
                w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
                w.recursiveSetEnabled(getShell(), true);
                w.getShell().setEnabled(!getShell().getEnabled());
                w.close();
             }
          });
       }
    } 
    abstract protected void endOperationImpl();
  } // end class MyOperationListener
  </code></pre>
  
  <p>Thanks for any help you can give me with this.</p>
  
  <p><em><strong>EDIT</em></strong></p>
  
  <p>Baz, your answer below is exactly what the question asked, so thank you for answering.</p>
  
  <p>But I am starting to think that what I am trying to do is not possible.
  When my operation starts, I wanted the progress bar indicator to start and when my operation ended I wanted the indicator be at the end and the monitor would close.</p>
  
  <p>I thought there might bee a way to use my listener to add the progressbar.  Something like the following.</p>
  
  <pre><code> public void startOperation(String startMessage) {
       Display.getDefault().asyncExec(new Runnable() {
          public void run() {
             -&gt;monitor.beginTask("Creating PDF File(s)", IProgressMonitor.UNKNOWN);&lt;-
             w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_WAIT));
             w.recursiveSetEnabled(getShell(), getShell().getEnabled());
             w.getShell().setEnabled(!getShell().getEnabled());
  
          }
       });
    } 
    public void endOperation() {
       try {
          -&gt;monitor.worked(1);&lt;-
          endOperationImpl();
       }
       finally {
          Display.getDefault().asyncExec(new Runnable() {
             public void run() {
                w.getShell().setCursor(new Cursor(Display.getCurrent(), SWT.CURSOR_ARROW));
                w.recursiveSetEnabled(getShell(), true);
                w.getShell().setEnabled(!getShell().getEnabled());
                -&gt;monitor.done();&lt;-
                w.close();
             }
          });
       }
    } 
    abstract protected void endOperationImpl();
  </code></pre>
  
  <p>} // end class MyOperationListener</p>
  
  <p>But I am starting to see that the ProgressBar has to have some sort of measurement to display the indicator correctly.</p>
  
  <p>I would be happy if the indicator just went back and forth and the monitor would close at the end of the operation.</p>