Invalid Thread Access Error with Java Swt

Invalid Thread Access Error with Java SWT

It is thrown because your listener code is called from outside the SWT Display thread. You run code on the display thread like this:

Display.getDefault().syncExec(new Runnable() {
public void run() {
// ...
}
});

or, asynchronously:

Display.getDefault().asyncExec(new Runnable() {
public void run() {
// ...
}
});

Java SWT and Invalid Thread Access

List is an SWT widget and if you call the getItems() method on it outside of the UI Thread (in this case your main thread), you get an ERROR_THREAD_INVALID_ACCESS SWTException. This is defined in the List API: ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

The Thread that created the receiver, is the the thread that created the Display. If a Display does not exist, the first call to Display.getDefault() creates one. Therefore your main thread, which calls the open() method, is the UI thread. Your code will work if you wrap the contents of the threadMethod():

private void threadMethod() {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// threadMethod contents
}
});
}

It will then be executed in the UI thread.

Eclipse Oxygen fails to run - org.eclipse.swt.SWTException: Invalid thread access

I've just commented on bug 477247. I'm getting a similar exception (on OSX). What allows me to start Eclipse without reinstalling is to delete the configuration/org.eclipse.osgi folder. It looked like a bundle cache, and deleting such caches has worked elsewhere for me. Hopefully this might also provide a clue for the devs.

Invalid Thread Access

You are calling dispose() on the Display object - never do that in Eclipse plugins.

If you want to draw in the view area itself just use a Canvas control, something like:

public void createPartControl(Composite parent)
{
// Create control

final Canvas canvas = new Canvas(parent, SWT.NONE);

// Set up a single paint listener

canvas.addPaintListener(... paint the canvas );

// Schedule a redraw after 1000 milliseconds

Display.getCurrent().timerExec(1000, new Runnable() {
public void run()
{
if (!canvas.isDisposed())
{
canvas.redraw();

Display.getCurrent().timerExec(1000, this);
}
}
});
}

Invalid Thread Access error when changing Java SWT Label

This link from the SWT FAQ explains the error and how to solve it: any code that modifies GUI components (in your case, setting the text of the label) needs to run on the display thread, otherwise this error will occur.

To run on the display thread, wrap the code inside a Runnable and call Display.getDefault().syncExec( with the provided Runnable:

Display.getDefault().syncExec(new Runnable() {
public void run() {
// code that affects the GUI
}
});

Java: Invalid thread access - pop up menu

The reason you are getting that exception is because when you make changes that are UI related with SWT you have to execute a runnable vis Display.syncExec(myRunable) otherwise that exception will occur.



Related Topics



Leave a reply



Submit