VINEEL
VINEEL

Reputation: 125

Thread Invalid Access Error in SWT

Could you let me know the reason for this error in SWT "org.eclipse.swt.SWTException" Invalid Thread access ? And How to fix such errors.

Upvotes: 2

Views: 1142

Answers (2)

Mario Marinato
Mario Marinato

Reputation: 4617

It happens when you try to act upon an interface item from a thread that's not the UI thread.

To run a code on the UI thread you have to use a Runnable and ask the display thread to run it. This way:

Display.getDefault().syncExec( new Runnable() {
    @Override
    public void run() {
        // Do your job here
    }
} );

As stated by the syncExec method javadoc,

the thread which calls this method is suspended until the runnable completes.

Also, you might check the asyncExec method.

Upvotes: 4

mtraut
mtraut

Reputation: 4740

In SWT you can access GUI resources only from the display thread. For example when setting the text in a org.eclipse.swt.widgets.Text control you must already be in the display thread or call


        final Text text = ...;
        Display.getCurrent().syncExec(new Runnable() {
            @Override
            public void run() {
                text.setText("test");
            }
        });

Upvotes: 2

Related Questions