DWT multi thread issue
Eric Suen
eric.suen.tech at gmail.com
Fri Oct 31 10:09:58 PDT 2008
shell.setText( "wait " ~ to!(String)(i++) );
only works on GUI thread, otherwise it will throw exception,
you should using display.synExec(...); make sure it be called
in GUI thread.
> I ported a SWT multi thread sample to D, but it not worked, and body help?
> tested with recent dwt-win on Windows XP, compile by DMD 1.036, click on the
> button, nothing happen:
>
>
> import dwt.DWT;
> import dwt.widgets.Listener;
> import dwt.widgets.Event;
> import dwt.custom.BusyIndicator;
> import dwt.events.SelectionAdapter;
> import dwt.events.SelectionEvent;
> import dwt.layout.GridData;
> import dwt.layout.GridLayout;
> import dwt.widgets.Button;
> import dwt.widgets.Display;
> import dwt.widgets.List;
> import dwt.widgets.Shell;
>
> import tango.core.Thread;
> import tango.time.WallClock;
> import tango.util.Convert;
> import tango.io.Stdout;
>
> import dwt.dwthelper.utils;
>
>
> /**
> * @author tenyears.cn
> */
> public class SWTThread {
> private int size = 10; //
> private long runTime = 50000; // 50 seconds
> private List list;
> private Shell shell;
>
> void dgRefresh()
> {
> Thread thread = new Thread( {new RunOne();} );
> thread.start();
> }
>
> class RunOne : Runnable
> {
> uint i;
> void run()
> {
> long start = WallClock.now.span.millis;
> shell.setText( "wait " ~ to!(String)(i++) );
> while ((WallClock.now.span.millis() - start) < runTime)
> {
> shell.setText( "wait " ~ to!(String)(i++) );
> Thread.sleep(10);
> }
> }
> }
>
> public void startThread()
> {
> try
> {
> Runnable refresh = dgRunnable(&dgRefresh);
> BusyIndicator.showWhile(shell.getDisplay(), refresh);//ÕâÒ»¾äºÜ¹Ø¼ü
> }
> catch(Exception e)
> {Stdout.formatln("startThread catch {}", e.msg);}
> }
>
>
> void onStartBtn(Event event)
> {
> try
> {
> startThread();
> }
> catch(Exception e)
> {Stdout.formatln("onStartBtn catch {}", e.msg);}
>
> }
>
> public Shell open(Display display)
> {
> shell = new Shell(display, DWT.DIALOG_TRIM | DWT.MIN);
> shell.setText("SWT Thread Test");
> shell.setLayout(new GridLayout(1, true));
> list = new List(shell, DWT.BORDER);
> list.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true));
> for (int index = 0; index < size; index++)
> list.add("String " ~ to!(String) (index + 1));
> Button startBtn = new Button(shell, DWT.PUSH);
> startBtn.setLayoutData(new GridData(DWT.FILL, DWT.CENTER, true,
> false));
> startBtn.setText("Start");
>
> startBtn.addListener(DWT.Selection, dgListener(&onStartBtn));
> shell.setSize(400, 300);
> shell.open();
> return shell;
> }
>
>
> }
>
> public void main(String[] args) {
> Display display = new Display();
> SWTThread application = new SWTThread();
> Shell shell = application.open(display);
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
> display.dispose();
> }
>
>
> SWT code:
> http://blog.csdn.net/baobeiSimple/archive/2007/08/22/1753910.aspx
>
>
>
>
> --
> yidabu <yidabu.spam at gmail.com>
> http://www.dsource.org/projects/dwin
>
> D ÓïÑÔ-ÖÐÎÄ(D Chinese):
> http://www.d-programming-language-china.org/
> http://bbs.d-programming-language-china.org/
> http://dwin.d-programming-language-china.org/
> http://scite4d.d-programming-language-china.org/
>
>
More information about the Digitalmars-d-dwt
mailing list