Thread overview
Timer in D.API?
Mar 04, 2009
Sam Hu
Mar 04, 2009
Christopher Wright
Mar 05, 2009
Sam Hu
Mar 05, 2009
Christopher Wright
March 04, 2009
First of all,this is about D ,not C#.
In C# the program print each letter of a string per 0.3 second one by one using the Timer & delegate:
C# code:
using System;
using System.Text;
using System.Timers;

namespace OneLetterATime
{
    class Program
    {
        static int counter = 0;
        static string displayString = @"This string will appear one letter at a time.";
        static void Main(string[] args)
        {
            Timer myTimer = new Timer(300);
            myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
            myTimer.Start();
            Console.ReadKey();


        }
        static void WriteChar(Object sender, ElapsedEventArgs e)
        {
            Console.Write(displayString[counter++%displayString.Length]);
        }
    }
I would like to implement the same demand using D+Tango:
D code:
module OneLetterATime;

import tango.io.Stdout; import tango.core.Thread; //import samsTools.PromptMessage;

int main(char[][] args)
{
  static int counter=0;
  static char[] displayString=r"This string will appear one letter at a time.";
  void writeChar()
  {
    Thread thisThread=Thread.getThis;
    for(int counter=0;;counter++)
    {
      Stdout.format("{}",displayString[counter%displayString.length]).flush;
      thisThread.sleep(0.3);
    }

  }
  Thread thread=new Thread(&writeChar);
  thread.start;

  return 0;
}
But I am not sure whether this is the right D way to meet the goal.Also I would like to know how to abort the program in D as the same does in the above C# program that the program aborts when the user press any key(Console.ReadKey();)?Finally I would like to know is there any API in D or Tango that does the same job as Timer in Windows?

Thank so much for your help.

Regards,
Sam
March 04, 2009
On Tue, 03 Mar 2009 23:10:07 -0500, Sam Hu wrote:

> First of all,this is about D ,not C#. In C# the program print each
> letter of a string per 0.3 second one by one using the Timer & delegate:
> C# code:
> using System;
> using System.Text;
> using System.Timers;
> 
> namespace OneLetterATime
> {
>     class Program
>     {
>         static int counter = 0;
>         static string displayString = @"This string will appear one
>         letter at a time."; static void Main(string[] args)
>         {
>             Timer myTimer = new Timer(300);
>             myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
>             myTimer.Start();
>             Console.ReadKey();
> 
> 
>         }
>         static void WriteChar(Object sender, ElapsedEventArgs e) {
>             Console.Write(displayString[counter++%
displayString.Length]);
>         }
>     }
> I would like to implement the same demand using D+Tango: D code:
> module OneLetterATime;
> 
> import tango.io.Stdout;
> import tango.core.Thread;
> //import samsTools.PromptMessage;
> 
> int main(char[][] args)
> {
>   static int counter=0;
>   static char[] displayString=r"This string will appear one letter at a
>   time."; void writeChar()
>   {
>     Thread thisThread=Thread.getThis;
>     for(int counter=0;;counter++)
>     {
>       Stdout.format("{}",displayString[counter%
displayString.length]).flush;
>       thisThread.sleep(0.3);
>     }
> 
>   }
>   Thread thread=new Thread(&writeChar); thread.start;
> 
>   return 0;
> }
> But I am not sure whether this is the right D way to meet the goal.

It depends on how accurate you want it.  Sleeping is guaranteed to sleep for at least the amount of time asked for, but it can sleep for longer. So it's possible your characters will come out slightly slower.  To account for this, you need to use the current time to adjust your sleep time.

e.g.:
import tango.time.Clock;
...
auto curTime = Clock.now;
auto period = TimeSpan.fromMilliseconds(300);
auto nextTime = curTime + period;
for(int counter=0;;counter++)
{
  Stdout(displayString[counter%displayString.length]).flush;
  auto timeToSleep = nextTime - curTime;
  if(timeToSleep > TimeSpan.zero)
    Thread.sleep(timeToSleep.interval);
  curTime = Clock.now;
  nextTime += period;
}

> Also
> I would like to know how to abort the program in D as the same does in
> the above C# program that the program aborts when the user press any
> key(Console.ReadKey();)?

tango.stdc.stdlib.exit(int retval)
This will terminate all threads in the process.

> Finally I would like to know is there any API in
> D or Tango that does the same job as Timer in Windows?
> 

As far as I know, there is no such class in Tango.
Not sure about other libs.

-Steve
March 04, 2009
Steve Schveighoffer wrote:
> e.g.:
> import tango.time.Clock;
> ...
> auto curTime = Clock.now;
> auto period = TimeSpan.fromMilliseconds(300);
> auto nextTime = curTime + period;
> for(int counter=0;;counter++)
> {
>   Stdout(displayString[counter%displayString.length]).flush;
>   auto timeToSleep = nextTime - curTime;
>   if(timeToSleep > TimeSpan.zero)
>     Thread.sleep(timeToSleep.interval);
>   curTime = Clock.now;
>   nextTime += period;
> }

I think the request was for a more event-like mechanism for timers.
March 05, 2009
Thank you both so much.
In DFL there is a Timer class but I can not get it work:
module dflTimer;

import dfl.all;

import tango.io.Stdout;

class LetterPerSecond
{
private:
Timer timer;
static int counter;
static char[] displayString;

void writeChar(Timer sender,EventArgs ea)
{
Stdout.format("{}",displayString[counter++%displayString.length]);
}

void startTimer() { timer=new Timer; timer.interval=300; timer.tick~=&this.writeChar; timer.start; } public:

static this() { counter=0; displayString=r"This string will appear one letter at a time."; } this() { Stdout.formatln("Before timer is starting..."); startTimer; Stdout.formatln("After timer is ended..."); } } int main(char[][] args) { auto prog=new LetterPerSecond; return 0; }

It compiled but just print 2 lines of message:
Before timer ...
After timer...
I am asking Chris Miller on DFL forum regarding this issue.

In DWT,there is a Runnable class I think it can gain the same result ,but not test yet.

Regards,
Sam
March 05, 2009
Sam Hu wrote:
> Thank you both so much.
> In DFL there is a Timer class but I can not get it work:
> It compiled but just print 2 lines of message:
> Before timer ...
> After timer...
> I am asking Chris Miller on DFL forum regarding this issue.
> 
> In DWT,there is a Runnable class I think it can gain the same result ,but not test yet.
> 
> Regards,
> Sam

I'm not sure about that Timer class, but in D, a program exits when main exits, even if there are background threads. You'd need some sort of Thread.join in your main function.