www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I create a fileWatcher with an onFileChange event using spawn?

reply Enjoys Math <enjoysmath gmail.com> writes:
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.
Aug 25
parent reply Jacob Carlborg <doob me.com> writes:
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 -- /Jacob Carlborg
Aug 27
next sibling parent reply Nemanja Boric <4burgos gmail.com> writes:
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
In addition, to avoid polling, it's possible to register yourself to the operating system so it will tell you when a modification on the given file has happened: https://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx?f=255&M PPError=-2147217396 http://man7.org/linux/man-pages/man7/inotify.7.html
Aug 27
parent Jacob Carlborg <doob me.com> writes:
On 2017-08-28 08:31, Nemanja Boric wrote:
 On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:
 http://code.dlang.org/packages/vibe-core
 http://code.dlang.org/packages/libasync
In addition, to avoid polling, it's possible to register yourself to the operating system so it will tell you when a modification on the given file has happened: https://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx?f=255&M PPError=-2147217396 http://man7.org/linux/man-pages/man7/inotify.7.html
That's what the two libraries above provides, in a cross-platform way. -- /Jacob Carlborg
Aug 28
prev sibling parent Enjoys Math <enjoysmath gmail.com> writes:
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.
Aug 28