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,
External threads can't access GUI. Check display.asyncExec.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With