Friday, January 25, 2008

How to threat threads right when in the wrong thread

Not sure if it is only me, but sometimes you are in a display thread and you need to stop to do others gui things like open a dialog and ask the user something, but there is confusion about wrong thread access exceptions and if not that, then perhaps, the thing doesn't stop were it should and... and... if you feel like that, like panic, just sit down again, take a deep break and look what good guys can show you (not me, but the eclipse guys :))

goGo = false;

// create a thread and inside do a syncExec
Thread thread = new Thread(){
public void run() {
Display.getDefault().syncExec(new Runnable(){
public void run() {
SomeDialog someDialog = new SomeDialog();;
goGo = true;

// wait for the dialog to finish
while( !goGo ) {
try {
} catch (InterruptedException ex) {

and then finally do whatever you have to do with the asked result.

No comments: