Eclipse ProgressMonitor在不同的线程中访问UI

时间:2014-05-22 08:52:27

标签: java eclipse swt

我需要在eclipse rcp app中显示进度条,以完成长任务 该任务还使用GUI组件。

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*

因为它与我获得的线程不同

  

引起:org.eclipse.swt.SWTException:无效的线程访问

1 个答案:

答案 0 :(得分:0)

您无法从其他线程访问显示线程。 使用:

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

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

您也可以使用syncExec。在这种情况下,调用线程将被解除,直到Runnable返回

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