Display.asyncExec vs Display.timerExec

I was hoping that Display.timerExec(int,Runnable) would be pretty much the same as Display.asyncExec(Runnable) but with the specified delay. However, it turns out that Display.timerExec can only be executed on the GUI thread, since its first line is a call to checkDevice() . This throws an exception if you are not working in a GUI thread.

Can anyone suggest a tool for using Display.asyncExec() , but with a delay before execution?

+4
source share
1 answer

You can first switch to a GUI thread using asyncExec , and then schedule a timer using timerExec . These two methods are similar in that they both perform some action, but asyncExec only switches the thread, timeExec only schedules the action for the GUI thread.

 display.asyncExec(() -> display.timerExec(100, () -> doThings())); 

It uses lambda expressions that are introduced in Java 8.

With Java 7 or earlier, you must write it with anonymous classes, for example:

 display.asyncExec(new Runnable() { public void run() { display.timerExec(100, new Runnable() { public void run() { doThings(); } }); } }); 
+6
source

Source: https://habr.com/ru/post/1400435/


All Articles