Runtime problems with HelloWorldSWT eclipse
I just downloaded Eclipse and was doing the HelloWorldSWT tutorial to familiarize myself with the IDE.
The program runs, but contrary to having display.sleep() in the main loop, the program continues to eat up CPU cycles. Is this normal for this tutorial? and how can I prevent this?
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorldSWT {
/**
* @param args
*/
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello world!");
shell.open();
while(!shell.isDisposed()){
display.sleep();
}
display.dispose();
shell.close();
}
}
Edit: I've found that
if (!display.r开发者_如何学运维eadAndDispatch()) {display.sleep();}
resolves the issue. However, I still do not understand the difference between checking for nonexistant events and then telling the display to sleep and just telling the display to sleep that would cause this issue.
You need to call display.readAndDispatch()
to read the events from the event queue and act on them (dispatch).
Even the the deactivate event from the Shell
must be dispatched!
All SWT based application have an event loop as the one you have added to your post. Have a look at the SWT Snippets for more examples.
It is how you create the project that is causing this. The correct relationships need to be applied at the start with dependencies set and libraries set.
I got it working then played around with it to see what was underlying cause as there are a lot of blogs with various tips that worked with some people and not with others.
Main point is this is necessary
import org.eclips.swt.widget.Display
import org.eclips.swt.widget.Screen
精彩评论