Tuesday, September 21, 2010

org.eclipse.swt.SWTException: Invalid threadaccess - Two days of follow up and finally its done :

This exception has been nagging up the problem with creation of JFreeChart with the RCP. The reason is that its not allowed to access the UI thread directly; you have to wrap it in something like this: (Source)



new Thread(new Runnable() {
public void run() {
while (true) {
try { Thread.sleep(1000); } catch (Exception e) { }
Display.getDefault().asyncExec(new Runnable() {
public void run() {
... do any work that updates the screen ...
}
}
}
}
}).start();



Ok, but the case is different in our case in the way that we already have actionPerformed method and the update has to be perfomed after getting the value from the database every one second or so. So, in above case, we dont need to create different thread, nor we need code for the timer. So, in our project, this will look something like this:


performDbConnection();
final long f = dataBase.returnNoOfDataset();

// this will only use the current display
try
{
Display.getDefault().asyncExec (new Runnable ()
{
public void run ()
{
series.addOrUpdate(new Millisecond(), f);
}
});
}
catch(Exception E)
{
System.out.println("Exception while updating the chart dynamically: " + e.toString() + "\n");
}


No comments:

Post a Comment