Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT single threading problem

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,

like image 226
Mohammad Kotb Avatar asked May 14 '26 01:05

Mohammad Kotb


2 Answers

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

like image 132
Ahmed Abdelkader Avatar answered May 15 '26 13:05

Ahmed Abdelkader


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.

like image 40
JesperE Avatar answered May 15 '26 13:05

JesperE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!