I have an eclipse rcp application. And I have a command when this command is executing. I need to start a thread. After execution of this thread GUI must be updated. But I suppose that this thread or other non-SWT thread cannot update GUI. But it seems reasonable. When I was trying to do that I got Exception in thread "Thread-5" org.eclipse.swt.SWTException: Invalid thread access
. How I can make this goal?
Using SWT you need to have anything that updates the GUI be done on the main thread, or in Eclipse, it's called the UI thread (it's the same thread). You are getting this error because you are trying to access an SWT object on another thread. Consider using Display.syncExec()
or Display.asyncExec()
to move the SWT related processing to the main thread. You want to be careful with syncExec()
that you don't cause a deadlock.
Normally I would not use java.lang.Thread in an RCP application, since Eclipse provides different level of abstraction and control over long running operations via the Jobs API (https://eclipse.org/articles/Article-Concurrency/jobs-api.html). Here is a solution for your problem:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With