www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Timer in D.API?

reply Sam Hu <samhu.samhu gmail.com> writes:


the Timer & delegate:

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

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
Mar 03 2009
parent reply Steve Schveighoffer <schveiguy yahoo.com> writes:
On Tue, 03 Mar 2009 23:10:07 -0500, Sam Hu wrote:


 letter of a string per 0.3 second one by one using the Timer & delegate:

 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

 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
Mar 04 2009
parent reply Christopher Wright <dhasenan gmail.com> writes:
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.
Mar 04 2009
parent reply Sam Hu <samhu.samhu gmail.com> writes:
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
Mar 04 2009
parent Christopher Wright <dhasenan gmail.com> writes:
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.
Mar 05 2009