digitalmars.D.learn - How do I create a fileWatcher with an onFileChange event using spawn?
- Enjoys Math (20/20) Aug 25 2017 Something like this:
- Jacob Carlborg (7/38) Aug 27 2017 A delegate perhaps?
- Nemanja Boric (5/42) Aug 27 2017 In addition, to avoid polling, it's possible to register yourself
- Jacob Carlborg (4/13) Aug 28 2017 That's what the two libraries above provides, in a cross-platform way.
- bauss (3/15) Nov 13 2017 There's already a dub package for file system watching:
- Enjoys Math (4/41) Aug 28 2017 No a plain delegate won't work. There's something you're not
- shuji (11/31) Nov 12 2017 @Nemanja Boric I would not recommend calling those APIs on
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 2017
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 2017
On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:On 2017-08-25 23:25, Enjoys Math wrote: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.htmlSomething 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
Aug 27 2017
On 2017-08-28 08:31, Nemanja Boric wrote:On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:That's what the two libraries above provides, in a cross-platform way. -- /Jacob Carlborghttp://code.dlang.org/packages/vibe-core http://code.dlang.org/packages/libasyncIn 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 28 2017
On Monday, 28 August 2017 at 11:25:03 UTC, Jacob Carlborg wrote:On 2017-08-28 08:31, Nemanja Boric wrote:There's already a dub package for file system watching: https://code.dlang.org/packages/fswatchOn Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:That's what the two libraries above provides, in a cross-platform way.http://code.dlang.org/packages/vibe-core http://code.dlang.org/packages/libasyncIn 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
Nov 13 2017
On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote:On 2017-08-25 23:25, Enjoys Math wrote: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.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
Aug 28 2017
On Friday, 25 August 2017 at 21:25:37 UTC, 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.Nemanja Boric I would not recommend calling those APIs on Windows because they work on entire directories, not suitable for individual files and sometimes those functions not even work when other programs change the files in a non standard way (that has happened to me before when editing through a text editor) Enjoys Math The way I usually deal with this problem is I save the filename, the time and handler function in an associative array and loop through when something changes, this way you can delete, sort, etc on the map when the files are deleted or so. Hope this helps a little.
Nov 12 2017