Thread overview |
---|
February 17, 2014 Timer | ||||
---|---|---|---|---|
| ||||
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; } |
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | 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 |
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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.
|
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | 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);
|
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | 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.
|
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | 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 |
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris | 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;
}
|
February 17, 2014 Re: Timer | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | 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).
|
Copyright © 1999-2021 by the D Language Foundation