views:

114

answers:

2

Hi,

I faced a problem while using threading for the first time, In an SWT program in the main thread I have created the GUI and opened the shell, and then a new thread is started to run some logic in the model, and in the model at a certain state there is a method is called in the GUI class... and here it is the problem this method is called in the 2nd thread while I want it to be called in the main thread or at least execute it in the main thread

How can I solve this problem? Thanks,

+3  A: 

External threads can't access GUI. Check display.asyncExec.

ahmadabdolkader
http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/swt_threading.htm
ahmadabdolkader
http://book.javanb.com/swt-the-standard-widget-toolkit/ch05lev1sec7.html
ahmadabdolkader
+2  A: 

You need to use the asyncExec or syncExec methods in the Display class in order to execute a runnable in the main thread:

// do stuff in a background thread

// ...then schedule job to run in main thread
display.asyncExec(new Runnable() {
   ...
});

Both syncExec and asyncExec will schedule a job in the main (UI) thread as soon as possible. The difference is that asyncExec returns immediately, while syncExec will not return until the job has completed.

JesperE