Mohammad Kotb
Mohammad Kotb

Reputation: 595

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,

Upvotes: 3

Views: 569

Answers (2)

JesperE
JesperE

Reputation: 64404

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.

Upvotes: 3

Ahmed Abdelkader
Ahmed Abdelkader

Reputation: 1735

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

Upvotes: 3

Related Questions