Reading file contents when file has changed

Kevin Cox kevincox.ca at gmail.com
Fri Jul 13 13:47:01 PDT 2012


On Jul 13, 2012 4:40 PM, "OlaOst" <olaa81 at gmail.com> wrote:
>
> I'm working on a program (using dmd 2.059 under windows) that
automatically reloads the contents of a file if it has changed, by checking
the last modified timestamp on the file every 0.2 seconds, then using the
readText function in std.file to read in the file contents.
>
> It works pretty well, but every once in a while I get a "The process
cannot access the file because it is in use by another process" error
message. I guess this happens when my D program tries to read the file
before the text editor manages to close its handle on the file.
>
> This would be fair enough if I was writing to the file in my D program,
but I only want to read from it. Is this a bug or is there a better way to
get file contents in D?

While the text editor is writing the file reading it is a silly thing to do
(no one knows what's there).  I'm not familiar with windows but locking
under Linux works as so.  Any number or processes can have a read lock but
if there is a write lock only the single process can access that file.

Also isn't there something like inotify on windows?  Polling sounds like
the wrong solution.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120713/1f1c006c/attachment.html>


More information about the Digitalmars-d-learn mailing list