Reputation: 993
I've been working on an Eclipse-Plugin for a while (my first one, everything is very new).
Now I've realized that I have to add threading by using background jobs, because I don't want the GUI to be frozen during the long processes in my Plugin. But because I didn't think (or know) about this when I started coding, I have multiple GUI-accesses sprinkled all over the long-running processes, therefore I can't simply put them into a job.
Is there any way I can, from inside a job, make those GUI-acccesses to be processed in the Main thread? Otherwise I will have to change the structure of my whole Plugin which is kinda a lot of work.
Upvotes: 0
Views: 481
Reputation: 111162
You can use the Display
asyncExec
or syncExec
methods to run a Runnable
in the UI thread from your job.
Display.getDefault().asyncExec(runnable);
asyncExec
just schedules the runnable to run as soon as possible. syncExec
blocks until the runnable has executed.
If you are using Java 8 you can use a lambda for the runnable:
Display.getDefault().asyncExec(() -> { code });
If you want to do something like prompt for a value use a Runnable
class that saves the result and use syncExec
to run it:
class PromptRunnable implements Runnable
{
private String result;
public void run()
{
result = .... some prompt dialog
}
String getResult()
{
return result;
}
}
PromptRunnable doPrompt = new PromptRunnable();
Display.getDefault().syncExec(doPrompt);
String result = doPrompt.getResult();
Upvotes: 1