Thread overview
Timer in DWT?
Sep 08, 2008
Sam Hu
Sep 08, 2008
Bill Baxter
Sep 08, 2008
Sam Hu
Sep 08, 2008
Frank Benoit
Sep 09, 2008
Sam Hu
Sep 09, 2008
Bill Baxter
Sep 09, 2008
Sam Hu
Sep 09, 2008
Frank Benoit
Sep 09, 2008
Sam Hu
Sep 09, 2008
Bill Baxter
September 08, 2008
Hi Frank,

Just a stupid question:is there a timer control in DWT?If no,then how to implement the same ?

Thanks,
Regards,
Sam
September 08, 2008
On Mon, Sep 8, 2008 at 4:42 PM, Sam Hu <samhu.samhu@gmail.com> wrote:
> Hi Frank,
>
> Just a stupid question:is there a timer control in DWT?If no,then how to implement the same ?
>
> Thanks,
> Regards,
> Sam
>

What's a timer control in your parlance?
Is that like a progress meter?
Or do you mean just a timer object that can trigger a callback after a
fixed interval (and not a GUI control)?
Or something else?

--bb
September 08, 2008
Thanks BB,

I mean just a timer object or something like that which  can trigger a callback after a fixed interval (and not a GUI control).

Sam

September 08, 2008
Bill Baxter schrieb:
> On Mon, Sep 8, 2008 at 4:42 PM, Sam Hu <samhu.samhu@gmail.com> wrote:
>> Hi Frank,
>>
>> Just a stupid question:is there a timer control in DWT?If no,then how to implement the same ?
>>
>> Thanks,
>> Regards,
>> Sam
>>
> 
> What's a timer control in your parlance?
> Is that like a progress meter?
> Or do you mean just a timer object that can trigger a callback after a
> fixed interval (and not a GUI control)?
> Or something else?
> 
> --bb

I would understand that as the callback thing.
There is in dwt.widgets.Display
public void timerExec(int milliseconds, Runnable runnable)

Search through the dwt-samples and you will find several hits.

Tip: Use the dgRunnable template function:
Display.getCurrent.timerExec( 500, dgRunnable({
  // handler code
}));


September 09, 2008
Hi Frank,

For example,I want to draw a line of text on the form which indicate the current date and time,very second it changes.Below is the code I tried,when I run the program,the drawn text does not change every second if I won't resize the form.I think it is the canvas.redraw method which is the problem,but I don't know which method shoud I use instead.
//*************************
 Display display=new Display;
 Shell shell=new Shell(display);
 Canvas canvas=new Canvas(shell,DWT.NONE);
 canvas.addPaintListener(new class PaintListener{
    public void paintControl(PaintEvent e)
    {
       auto layout=new Locale;

      e.gc.drawText(layout("{:ddd,dd MMMM yyyy HH':'mm':'ss z}",
				WallClock.now),100,100);
    }
 });

display.getCurrent.timerExec( 500, dgRunnable({

					canvas.redraw;
  }));

//*****************************

Please help.Thanks.

Sam
September 09, 2008
On Tue, Sep 9, 2008 at 9:41 AM, Sam Hu <samhu.samhu@gmail.com> wrote:
> Hi Frank,
>
> For example,I want to draw a line of text on the form which indicate the current date and time,very second it changes.Below is the code I tried,when I run the program,the drawn text does not change every second if I won't resize the form.I think it is the canvas.redraw method which is the problem,but I don't know which method shoud I use instead.
> //*************************
>  Display display=new Display;
>  Shell shell=new Shell(display);
>  Canvas canvas=new Canvas(shell,DWT.NONE);
>  canvas.addPaintListener(new class PaintListener{
>    public void paintControl(PaintEvent e)
>    {
>       auto layout=new Locale;
>
>      e.gc.drawText(layout("{:ddd,dd MMMM yyyy HH':'mm':'ss z}",
>                                WallClock.now),100,100);
>    }
>  });
>
> display.getCurrent.timerExec( 500, dgRunnable({
>
>                                        canvas.redraw;
>  }));
>
> //*****************************

This only executes the timer once.  You have to call timerExec again in the delegate to get it to display again.  So I think you /may/ need to go ahead and create a Runnable subclass to make it work.  Like this:

class EverySecond : Runnable
{
    void run() {
        canvas.redraw;
        display.getCurrent.timerExec(500, this);
    }
}

That may also require you to make canvas and display members of EverySecond.

--bb
September 09, 2008
Thanks BB,now it works when the run() method is invoked in the InitializeComponents() method,but it does not work when invoked in the ctor.How come? Thanks,Sam

class MainForm:Runnable
{
  private:
  Display display;
  Shell shell;
  Canvas canvas;
  void InitializeComponents()
  {
   display=new Display;
   shell=new Shell(display);
   canvas=new Canvas(shell,DWT.NONE);
   canvas.addPaintListener(new class PaintListener{
    public void paintControl(PaintEvent e)
    {
       auto layout=new Locale;

     e.gc.drawText(layout("{:dddd,dd MMMM yyyy HH':'mm':'ss z}",
                                WallClock.now),100,100);
   }});
   run;
   shell.open;
  while(! shell.isDisposed)
 {
    if(! display.readAndDispatch)
    display.sleep;
 }
  display.dispose;
 }
 public:
  this()
  {
    InitializeComponents;
    run;//invoke run() method here does not work?
  }
 void run()
  {
     canvas.redraw;
     display.getCurrent.timerExec(1000, this);

  }

}
int main(char[][] args)
{
 MainForm mainForm=new MainForm;
 return 0;
}

September 09, 2008
Sam Hu schrieb:
> Thanks BB,now it works when the run() method is invoked in the InitializeComponents() method,but it does not work when invoked in the ctor.How come? Thanks,Sam
> 
> class MainForm:Runnable
> {
>   private:
>   Display display;
>   Shell shell;
>   Canvas canvas;
>   void InitializeComponents()
>   {
>    display=new Display;
>    shell=new Shell(display);
>    canvas=new Canvas(shell,DWT.NONE);
>    canvas.addPaintListener(new class PaintListener{
>     public void paintControl(PaintEvent e)
>     {
>        auto layout=new Locale;
> 
>      e.gc.drawText(layout("{:dddd,dd MMMM yyyy HH':'mm':'ss z}",
>                                 WallClock.now),100,100);
>    }});
>    run;
>    shell.open;
>   while(! shell.isDisposed)
>  {
>     if(! display.readAndDispatch)
>     display.sleep;
>  }
>   display.dispose;
>  }
>  public:
>   this()
>   {
>     InitializeComponents;
>     run;//invoke run() method here does not work?
>   }
>  void run()
>   {
>      canvas.redraw;
>      display.getCurrent.timerExec(1000, this);
> 
>   }
> 
> }
> int main(char[][] args)
> {
>  MainForm mainForm=new MainForm;
>  return 0;
> }
> 

the call to run() in the ctor is not executed, because the program is hanging in the InitializeComponents() while loop. Only if the window is closed, the loop is left and then the run is called ... too late.
September 09, 2008
Ah ~~ So stupid question...while(){}...

Thanks BB!
September 09, 2008
On Tue, Sep 9, 2008 at 3:50 PM, Sam Hu <samhu.samhu@gmail.com> wrote:
> Ah ~~ So stupid question...while(){}...
>
> Thanks BB!

FB gets the credit for that one.  :-)

--bb