digitalmars.D.learn - Timer
- Chris (5/5) Feb 17 2014 The D way of implementing a timer? I need to (automatically)
- simendsjo (5/10) Feb 17 2014 Vibe.d can be used for this to get an OS agnostic solution.
- Chris (3/14) Feb 17 2014 Great, it's for a vibe.d project anyway, it might do the trick.
- Dejan Lekic (43/48) Feb 17 2014 Here is a quick timer implementation that you can improve
- Chris (5/54) Feb 17 2014 Great stuff, thanks for the code example. I feel flattered to
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
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
On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote: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.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
On Monday, 17 February 2014 at 11:20:05 UTC, Chris wrote:On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:This works (in vibe.d): auto timer = setTimer(1.seconds, toDelegate(&clearDir), true);On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote: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.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
On Monday, 17 February 2014 at 11:20:05 UTC, Chris wrote:On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:You could have a separate thread used just for this that sleeps most of the time.On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote: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.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
Am 17.02.2014 12:20, schrieb Chris:On Monday, 17 February 2014 at 11:11:06 UTC, simendsjo wrote:https://github.com/Dav1dde/BraLa/blob/master/brala/utils/thread.d#L50-L95On Monday, 17 February 2014 at 11:08:16 UTC, Chris wrote: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.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
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
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: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).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