0

I need to display a progressbar in eclipse rcp app, for long task.
The task is also using GUI components.

ProgressMonitorDialog progBar = new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());

progBar.run(true, false, new IRunnableWithProgress(){ 
  public void run(IProgressMonitor monitor) { 
        editor.doTask(monitor);
   } 
});


public void doTask(IProgressMonitor monitor) {
  monitor.beginTask("Progress", IProgressMonitor.UNKNOWN); 
   theomposite.getValues(); // <- fails here *WRONG THREAD*

Because it's not the same thread I am getting

Caused by: org.eclipse.swt.SWTException: Invalid thread access

yuris
  • 1,109
  • 4
  • 19
  • 33
  • possible duplicate of [Invalid Thread Access Error with Java SWT](http://stackoverflow.com/questions/5980316/invalid-thread-access-error-with-java-swt) – Baz May 22 '14 at 09:03

1 Answers1

0

You can not access the display thread from another thread. Use:

org.eclipse.swt.widgets.Display.getDefault().asyncExec( new Runnable()
{
    @Override
    public void run()
    {
        // your stuff
    }
});

See http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FDisplay.html

You could also use syncExec. In this case, the calling thread will be supsended until the Runnable returns

See http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FDisplay.html

Mirco
  • 2,940
  • 5
  • 34
  • 57
  • Didn't work, i wrapped the call progBar.run() with it and got same error – yuris May 22 '14 at 09:11
  • @yuris You can't wrap the runnable in it, just the part that interacts with the UI, e.g. `theomposite.getValues()`. – Baz May 22 '14 at 09:52
  • @Baz, There is also an issue with returning value from the anonymous class... And using some "global" class to hold the values is not a good option – yuris May 25 '14 at 06:20
  • You can use a uijob and a jobchangelistener – Mirco May 25 '14 at 07:43
  • @yuris If you really need to return something, use a callback. – Baz May 25 '14 at 08:53