www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Timer

reply "Chris" <wendlec tcd.ie> writes:
The D way of implementing a timer? I need to (automatically) 
execute a function that performs a clean up, say every hour.

if (file.older than 1 hour) {
     remove;
}
Feb 17 2014
next sibling parent reply "simendsjo" <simendsjo gmail.com> writes:
On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
     remove;
 }
Vibe.d can be used for this to get an OS agnostic solution. Haven't used it myself, but this function seems to be what you are looking for: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/core.d#L342
Feb 17 2014
parent reply "Chris" <wendlec tcd.ie> writes:
On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:
 On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
    remove;
 }
Vibe.d can be used for this to get an OS agnostic solution. Haven't used it myself, but this function seems to be what you are looking for: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/core.d#L342
Great, it's for a vibe.d project anyway, it might do the trick. Out of interest, if there's a phobos way, let me know.
Feb 17 2014
next sibling parent "Chris" <wendlec tcd.ie> writes:
On Monday, 17 February 2014 at 11:20:05 UTC, Chris wrote:
 On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:
 On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
   remove;
 }
Vibe.d can be used for this to get an OS agnostic solution. Haven't used it myself, but this function seems to be what you are looking for: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/core.d#L342
Great, it's for a vibe.d project anyway, it might do the trick. Out of interest, if there's a phobos way, let me know.
This works (in vibe.d): auto timer = setTimer(1.seconds, toDelegate(&clearDir), true);
Feb 17 2014
prev sibling next sibling parent "simendsjo" <simendsjo gmail.com> writes:
On Monday, 17 February 2014 at 11:20:05 UTC, Chris wrote:
 On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:
 On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
   remove;
 }
Vibe.d can be used for this to get an OS agnostic solution. Haven't used it myself, but this function seems to be what you are looking for: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/core.d#L342
Great, it's for a vibe.d project anyway, it might do the trick. Out of interest, if there's a phobos way, let me know.
You could have a separate thread used just for this that sleeps most of the time.
Feb 17 2014
prev sibling parent David <d dav1d.de> writes:
Am 17.02.2014 12:20, schrieb Chris:
 On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:
 On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) execute
 a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
    remove;
 }
Vibe.d can be used for this to get an OS agnostic solution. Haven't used it myself, but this function seems to be what you are looking for: https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/core/core.d#L342
Great, it's for a vibe.d project anyway, it might do the trick. Out of interest, if there's a phobos way, let me know.
https://github.com/Dav1dde/BraLa/blob/master/brala/utils/thread.d#L50-L95
Feb 17 2014
prev sibling parent reply "Dejan Lekic" <dejan.lekic gmail.com> writes:
On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
     remove;
 }
Here is a quick timer implementation that you can improve yourself: import std.stdio; import core.thread; class ChrisTimer : Thread { private void delegate() funcToRun; private long timeToWait; private bool done = false; private int noSheep; private Duration howLong; // We could make any function a parameter to ChrisTimer if we had a // constructor like: // this(void delegate() dg, long ms) { this(long ms) { funcToRun = &doSomething; timeToWait = ms; howLong = dur!("msecs")(timeToWait); funcToRun = &doSomething; super(&run); } private void run() { while (isRunning && !done) { funcToRun(); sleep(howLong); } } // Example function that is just going to count sheep (up to 10). public void doSomething() { ++noSheep; writeln("Counted ", noSheep, " sheep."); if (noSheep >= 10) { done = true; } } } // ChrisThread class int main(string[] args) { auto ct = new ChrisTimer(2000); ct.start(); return 0; }
Feb 17 2014
parent "Chris" <wendlec tcd.ie> writes:
On Monday, 17 February 2014 at 12:03:39 UTC, Dejan Lekic wrote:
 On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote:
 The D way of implementing a timer? I need to (automatically) 
 execute a function that performs a clean up, say every hour.

 if (file.older than 1 hour) {
    remove;
 }
Here is a quick timer implementation that you can improve yourself: import std.stdio; import core.thread; class ChrisTimer : Thread { private void delegate() funcToRun; private long timeToWait; private bool done = false; private int noSheep; private Duration howLong; // We could make any function a parameter to ChrisTimer if we had a // constructor like: // this(void delegate() dg, long ms) { this(long ms) { funcToRun = &doSomething; timeToWait = ms; howLong = dur!("msecs")(timeToWait); funcToRun = &doSomething; super(&run); } private void run() { while (isRunning && !done) { funcToRun(); sleep(howLong); } } // Example function that is just going to count sheep (up to 10). public void doSomething() { ++noSheep; writeln("Counted ", noSheep, " sheep."); if (noSheep >= 10) { done = true; } } } // ChrisThread class int main(string[] args) { auto ct = new ChrisTimer(2000); ct.start(); return 0; }
Great stuff, thanks for the code example. I feel flattered to have my own ChrisTimer now. I'll see which implementation is better suited for the task (vibe.d's setTimer or my own thread based on your example).
Feb 17 2014