How do I create a fileWatcher with an onFileChange event using spawn?

Enjoys Math via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 28 08:12:10 PDT 2017


On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:
> On 2017-08-25 23:25, Enjoys Math wrote:
>> 
>> 
>> Something like this:
>> 
>> 
>> module file_watcher;
>> 
>> import std.concurrency;
>> import std.file;
>> import std.signals;
>> import std.datetime;
>> 
>> 
>> void fileWatcher(Tid tid, string filename, int loopSleep) {
>>      auto modified0 = timeLastModified(filename);
>> 
>>      while (true) {
>>          modified = timeLastModified(filename);
>> 
>>          if (modified > modified0) {
>>              modified0 = modified;
>>              //if (onFileChange !is null)
>>              //    onFileChange(receiver);
>>          }
>> 
>>          sleep(dur!"msecs"(loopSleep));
>>      }
>> }
>> 
>> 
>> But I'm not sure how to send the onFiledChange event.
>
> A delegate perhaps?
>
> Or you can look at any of the existing event driven libraries 
> that do this:
>
> http://code.dlang.org/packages/vibe-core
> http://code.dlang.org/packages/libasync

No a plain delegate won't work.  There's something you're not 
telling me because I've tried delegates.  They have to be shared 
or something, and that causes a big mess with my code.


More information about the Digitalmars-d-learn mailing list