Thursday, January 31, 2008

Solution to "Invalid thread access" in eRCP

In Embedded Rich Client Platform, i.e. eRCP, an "Invalid thread access" exception is produced when any class that extends ViewPart executes a new thread the conventional way in method
public void createPartControl(Composite parent)


What do I mean by "executes a new thread the conventional way" ? Well here's an example (let's say we have a class or subclass WeatherData which implements Runnable) :


// This will produce an "Invalid thread access" error
Thread thread = new Thread(new WeatherData());


To eliminate the "Invalid thread access" issue, use this instead :


parent.getDisplay().syncExec(new WeatherData());