www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - timerExec problem in dwt-win

reply torhu <no spam.invalid> writes:
This test doesn't print "OK" like I think it should, it just prints 
"ctor called".  It seems that timerExec doesn't work when called from 
other threads than the GUI thread.  The SWT docs seem to say that this 
should work.  Am I doing something wrong?

When doing the same thing in my real DWT app, I get an "Invalid thread 
access" exception, but I haven't put an exception handler into the 
secondary thread in this sample.

---
module timertest2;

import dwt.DWT;
import dwt.dwthelper.Runnable;
import dwt.widgets.Display;
import dwt.widgets.Shell;
import dwt.widgets.Button;
import dwt.widgets.Text;

import tango.core.Thread;
import dwt.events.SelectionListener;
import dwt.events.SelectionEvent;
import tango.util.log.Trace;


void main()
{
     try {
         Display display = new Display();
         Shell shell = new Shell(display);
         shell.setSize(300, 200);
         shell.setText(__FILE__);
         auto btn = new Button( shell, DWT.PUSH );
         btn.setBounds(40, 50, 100, 50);
         btn.setText( "test timerExec" );

         auto txt = new Text(shell, DWT.BORDER);
         txt.setBounds(170, 50, 100, 40);

         auto t = new Thread({Display.getDefault.timerExec(500, new 
class Runnable {
             this() { Trace.formatln("ctor called").flush; }
             void run() { Trace.formatln("OK").flush; 
txt.setText("timerExec OK"); }
         });});

         btn.addSelectionListener(new class () SelectionListener {
             void widgetSelected(SelectionEvent event) { t.start(); }
             void widgetDefaultSelected(SelectionEvent event) { }
         });

         shell.open();
         while (!shell.isDisposed()) {
             if (!display.readAndDispatch()) {
                 display.sleep();
                 Trace.format(".").flush;
             }
         }
     }
     catch (Exception e) {
         Trace.formatln(e.toString);
     }
}
---
Sep 27 2008
next sibling parent torhu <no spam.invalid> writes:
torhu wrote:
 This test doesn't print "OK" like I think it should, it just prints 
 "ctor called".  It seems that timerExec doesn't work when called from 
 other threads than the GUI thread.  The SWT docs seem to say that this 
 should work.  Am I doing something wrong?
 
 When doing the same thing in my real DWT app, I get an "Invalid thread 
 access" exception, but I haven't put an exception handler into the 
 secondary thread in this sample.
 

I made a version for the old DWT and phobos too. It behaves the same way, except that it actually prints "Invalid thread access". That's interesting, because the with new dwt and Tango, exceptions in secondary threads don't propagate to the main thread. They just seem to vanish.
Sep 27 2008
prev sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
torhu schrieb:
 This test doesn't print "OK" like I think it should, it just prints
 "ctor called".  It seems that timerExec doesn't work when called from
 other threads than the GUI thread.  The SWT docs seem to say that this
 should work.  Am I doing something wrong?
 
 When doing the same thing in my real DWT app, I get an "Invalid thread
 access" exception, but I haven't put an exception handler into the
 secondary thread in this sample.

You get the same exception also in your example code, but it is not catched, so the message got lost. You need to wrap your code in the Thread into another display.asyncExec( dgRunnable({}));
Sep 28 2008
parent torhu <no spam.invalid> writes:
Frank Benoit wrote:
 torhu schrieb:
 This test doesn't print "OK" like I think it should, it just prints
 "ctor called".  It seems that timerExec doesn't work when called from
 other threads than the GUI thread.  The SWT docs seem to say that this
 should work.  Am I doing something wrong?
 
 When doing the same thing in my real DWT app, I get an "Invalid thread
 access" exception, but I haven't put an exception handler into the
 secondary thread in this sample.

You get the same exception also in your example code, but it is not catched, so the message got lost.

I wonder why it's not caught, since it happens automatically with the old DWT. I think I tested this a while ago, but I can't quite remember if it was a DWT-specific problem, or if it happens with Tango without DWT too.
 You need to wrap your code in the Thread into another
 display.asyncExec( dgRunnable({}));
 

Yeah, I was hoping I could just fire off timerExecs without blocking the current thread by using syncExec, or having to use asyncExec which is more complicated. But from rereading the SWT docs, timerExec does behave correctly in DWT. I'll have to do this another way, then.
Sep 28 2008