FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Timer does not work in my program

 
Post new topic   Reply to topic     Forum Index -> DFL
View previous topic :: View next topic  
Author Message
samsam698



Joined: 10 Jan 2008
Posts: 63

PostPosted: Wed Mar 04, 2009 6:40 pm    Post subject: Timer does not work in my program Reply with quote

Morning all,
I would like to print a string out to the console one letter by one every n seconds.Below is my implementation:

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 two lines of message:
Before timer is starting...
After timer is ended

I have no idea about what is going on there.Please help.

Thanks and best regards,
Sam
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Thu Mar 05, 2009 11:03 pm    Post subject: Re: Timer does not work in my program Reply with quote

Timers depend on Application.run() so throw this call in there before your return from main.
Back to top
View user's profile Send private message
samsam698



Joined: 10 Jan 2008
Posts: 63

PostPosted: Sun Mar 08, 2009 6:47 pm    Post subject: Re: Timer does not work in my program Reply with quote

Chris Miller wrote:
Timers depend on Application.run() so throw this call in there before your return from main.


Thank you very much for your reply ,Miller!

But I am sorry I don't know where should I put the code in.I tried:

added in main:

auto prog=new LetterPerSecond;
Application.run;
return 0;

it does not work;
I also tried to add the the end of the startTimer method in the LetterPerSecond class ,but it does not work also.

So it would be grateful if you would like to paste a few lines of code samples here.

Regards,
Sam
Back to top
View user's profile Send private message
Chris Miller



Joined: 27 Mar 2004
Posts: 514
Location: The Internet

PostPosted: Tue Mar 10, 2009 3:07 pm    Post subject: Re: Timer does not work in my program Reply with quote

Works here... (switched to Phobos for quick test)

Code:
module dflTimer;

import dfl.all;

import std.stdio;

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

   void writeChar(Timer sender,EventArgs ea)
   {
      writefln("%s",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()
   {
      writefln("Before timer is starting...");
      startTimer;
      writefln("After timer is ended...");
   }
}
int main(char[][] args)
{
   auto prog=new LetterPerSecond;
   Application.run();
   return 0;
}
Back to top
View user's profile Send private message
samsam698



Joined: 10 Jan 2008
Posts: 63

PostPosted: Tue Mar 10, 2009 6:56 pm    Post subject: Reply with quote

Thank you very much,Chris!!It works now!
In my tango version,everything is ok except writeChar(...)method:
void writeChar(...)
{
Stdout.format(...);
here should be :
Stdout.format(...).flush;
}

BTW,I tested your phobos version with D2.x ,it works also.But I noticed that when I change writefln to writef/write,it does not work properly,ie,no printing letter on by one ,just the starting & ending msg.
module dflTimer;

import dfl.all;

import std.stdio;

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

void writeChar(Timer sender,EventArgs ea)
{
writef(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()
{
writefln("Before timer is starting...");
startTimer;
writefln("After timer is ended...");
}
}
int main(char[][] args)
{
auto prog=new LetterPerSecond;
Application.run();
return 0;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> DFL All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group