Wednesday 15 January 2014

java - SWT background thread blocking the GUI -



java - SWT background thread blocking the GUI -

below code blocking ui , have checked other posts , seems right implementation , please allow me know doing wrong here:

public class recordtest { public static boolean stopflag=false; public static void main(string[] args) { display display = display.getdefault(); shell shell = new shell (display); label label = new label (shell, swt.none); label.settext ("enter url:"); final text text = new text (shell, swt.border); text.setlayoutdata (new rowdata (100, swt.default)); button ok = new button (shell, swt.push); ok.settext ("start record"); thread updatethread = new thread() { public void run() { display.getdefault().asyncexec(new recorder(stopflag)); } }; // background thread updatethread.setdaemon(true); updatethread.start(); ok.addselectionlistener(new selectionadapter() { @override public void widgetselected(selectionevent e) { system.out.println("start record"); } }); button cancel = new button (shell, swt.push); cancel.settext ("stop recording"); cancel.addselectionlistener(new selectionadapter() { @override public void widgetselected(selectionevent e) { system.out.println("stop recording"); stopflag=true; } }); shell.setdefaultbutton (cancel); shell.setlayout (new rowlayout ()); shell.pack (); shell.open (); while (!shell.isdisposed ()) { if (!display.readanddispatch ()) display.sleep (); } display.dispose (); } }

------------------------------------recorder.java-----------------------------------------

public class recorder implements runnable { private boolean stopflag = false; public recorder(boolean stopflag) { this.stopflag = stopflag; } @override public void run() { (int = 0; < 10; i++) { seek { system.out.println("waiting ...."); thread.sleep(3000); } grab (exception e) { // todo auto-generated grab block e.printstacktrace(); } } } }

well, utilize display.asyncexec(...) in thread means code of recorder.run() runs in ui thread... of cause block event handling in ui thread.

the basic rule run method must finish within few milli-seconds!

java multithreading swt swt-awt

No comments:

Post a Comment