Eclipse RCP Multithreading

I have an eclipse rcp application. And I have a command when this command is executed. I need to start a stream. After executing this thread, the GUI should be updated. But I believe that this thread or another non-SWT thread cannot update the GUI. But that seems reasonable. When I tried to do this, I got it Exception in thread "Thread-5" org.eclipse.swt.SWTException: Invalid thread access. How can I do this task?

+3
source share
6 answers

Using SWT, you need to have something that updates the GUI in the main thread, or in Eclipse, it is called a UI thread (this is the same thread). You get this error because you are trying to access a SWT object in another thread. Consider using Display.syncExec()or Display.asyncExec()to move SWT-related processing to the main thread. You want to be careful syncExec()that you are not causing a dead end.

+5
source

, , , , API- Eclipse, . " eclipse"; : http://www.vogella.com/articles/EclipseJobs/article.html

+1

, , UI. Display.syncExec() Display.asyncExec() .

+1

java.lang.Thread RCP, Eclipse API- Jobs (https://eclipse.org/articles/Article-Concurrency/jobs-api.html). :

    Runnable uiUpdater = new Runnable() {
        public void run() {
            // update SWT UI safely here
            // ...
        }
    };
    Job longRunningOperation = new Job("My command") {
        protected IStatus run(IProgressMonitor monitor) {
            // some time consuming code here        
            // ...      
            Display.getDefault().asyncExec(uiUpdater);
            return Status.OK_STATUS;
        }
    };
    longRunningOperation.schedule();
+1

SWT! : GUI, , .

, - , , .

: :

Display.getDefautl().syncExec(new Runnable() {
   public void run() {
      // code related to GUI element(s)
   }
}

Display.getDefautl().asyncExec(new Runnable() {
   public void run() {
      // code related to GUI element(s)
   }
}

In the first case, execution is synchronous. The calling thread is waiting for the execution method to complete. In the second case, the calling thread does not wait.

To learn more about threads in an Eclipse application, look at the stack in your Debug view (in debug mode). The first thread, called the Main thread, is the user interface thread.

0
source

You should use Display.getDefault (). asyncExec () or Display.getDefautl (). syncExec ().

Code example:

@PostConstruct
public void createControls(Composite parent) {
   .... \\ label definition
}
public void updateInterface(String message)
    {
        try{
            Display.getDefault().asyncExec(new Runnable() {
              @Override
              public void run() {
                 try{
                        label.setText(message);  
                    }
                    catch(Exception exc){
                        System.out.println(exc);
                    }               
              }
            });
        }
        catch(Exception exception){
            System.out.println(exception);
        }   
    }
0
source

All Articles