Implementing a timer using threads

Heinz billgates at microsoft.com
Sun Jan 28 20:52:40 PST 2007


Dennis Kempin Wrote:

> Hi,
> 
> i just started to learn D (have been using c++ or java up to now) and am
> wondering how to implement some kind of timer, a thread that calls a
> delegate every n seconds.
> This was my first idea (without flags for stopping the timer etc):
> 
> class Timer: Thread
> {
>         int run()
>         {
>                 while(true)
>                 {
>                         this.wait(1000); // wait one second
>                         writefln("one second passed");
>                 }
>                 return 0;
>         }
> }
> 
> But writefln never gets executed, because this.wait is used to wait for
> other threads than the current one. Is there any other way to get let
> Thread sleep for some seconds (I know that there is a Sleep function for
> Win32, but a platform independend way would be very great).
> 
> regards
> Dennis

You could insert a block of asm code. Look here http://www.digitalmars.com/d/iasm.html for valid opcodes.
Here http://www.acm.uiuc.edu/sigmil/talks/shellcode/sleep.asm is an approach.

This method is platform independant but not architecture independant, anyway, most computers processors are x86.

Good luck.


More information about the Digitalmars-d-learn mailing list