DWT multi thread issue

yidabu yidabu.spam at gmail.com
Fri Oct 31 17:54:36 PDT 2008


DWT multi thread sample:

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.Shell;

import tango.core.Thread;
import tango.time.WallClock;
import tango.util.Convert;
import tango.io.Stdout;

import dwt.dwthelper.utils;
import dwt.dwthelper.Runnable;


/**
 * @author tenyears.cn
 * ported to D by D China
 * http://www.d-programming-language-china.org/
 * http://bbs.d-programming-language-china.org/
 */
public class SWTThread {
    private long runTime = 50000; // 50 seconds
    private Shell shell;      
    
    void dgRefresh()
    {
        Thread thread = new Thread( &setGuiText );
        thread.start();
    }

        void setGuiText()
        {
            uint i;
            long start = WallClock.now.span.millis;        
            void dgSetText(String text)
            {
                shell.setText(text);
            }
            while ((WallClock.now.span.millis() - start) < runTime) 
            {
                auto text = "wait " ~ to!(String)(i++);
                shell.getDisplay.syncExec( dgRunnable(&dgSetText, text) );
                
                Thread.sleep(1);
            }
        }
    
    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));
        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();
}




-- 
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