View previous topic :: View next topic |
Author |
Message |
samsam698
Joined: 10 Jan 2008 Posts: 63
|
Posted: Wed Mar 04, 2009 6:40 pm Post subject: Timer does not work in my program |
|
|
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 |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Thu Mar 05, 2009 11:03 pm Post subject: Re: Timer does not work in my program |
|
|
Timers depend on Application.run() so throw this call in there before your return from main. |
|
Back to top |
|
|
samsam698
Joined: 10 Jan 2008 Posts: 63
|
Posted: Sun Mar 08, 2009 6:47 pm Post subject: Re: Timer does not work in my program |
|
|
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 |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Tue Mar 10, 2009 3:07 pm Post subject: Re: Timer does not work in my program |
|
|
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 |
|
|
samsam698
Joined: 10 Jan 2008 Posts: 63
|
Posted: Tue Mar 10, 2009 6:56 pm Post subject: |
|
|
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 |
|
|
|