www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - DWT multi thread issue

reply yidabu <yidabu.spam gmail.com> writes:
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 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/
Oct 31 2008
next sibling parent Frank Benoit <keinfarbton googlemail.com> writes:
I cannot try it right now, but perhaps this works? :

void dgRefresh()
{
    auto runOne = new RunOne();
    Thread thread = new Thread( &runOne.run );
    thread.start();
}
Oct 31 2008
prev sibling next sibling parent "Eric Suen" <eric.suen.tech gmail.com> writes:
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 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/

 

Oct 31 2008
prev sibling next sibling parent yidabu <yidabu.spam gmail.com> writes:
On Fri, 31 Oct 2008 07:56:53 -0400
Frank Benoit <keinfarbton googlemail.com> wrote:

 I cannot try it right now, but perhaps this works? :
 
 void dgRefresh()
 {
     auto runOne = new RunOne();
     Thread thread = new Thread( &runOne.run );
     thread.start();
 }
 

code works: void dgRefresh() { auto runOne = new RunOne(); Thread thread = new Thread( &runOne.run ); thread.start(); } class RunOne : Runnable { uint i; void run() { Stdout("RunOne.run").newline; 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); } } } My question is : in dwt-samples\user\torhu_synctest.d, code works: auto t = new Thread({Display.getDefault.syncExec(new class Runnable { void run() { txt.setText("inside syncExec"); } });}); where code here not work: void dgRefresh() { auto runOne = new RunOne(); Thread thread = new Thread( { shell.getDisplay.syncExec(runOne); } ); thread.start(); } -- yidabu <yidabu.spam 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/
Oct 31 2008
prev sibling next sibling parent yidabu <yidabu.spam gmail.com> writes:
On Sat, 1 Nov 2008 01:09:58 +0800
"Eric Suen" <eric.suen.tech gmail.com> wrote:

 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.

Thanks.
 -- 
 yidabu <yidabu.spam 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/

 


-- yidabu <yidabu.spam 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/
Oct 31 2008
prev sibling parent yidabu <yidabu.spam gmail.com> writes:
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 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/
Oct 31 2008