I'm using Delphi and ReadDirectoryChangesW
(in a TThread
) to detect when a particular folder has changes. This is working quite nicely. I need to know when the changes are done so I can then safely do something with the file.
Here's the scenario: I've mapped a printer to a specific file name and folder (FILE: local port c:\MonitorMe\prinfil.dat
). The only file in the folder will be that printer file. The print job can be single page, or hundreds of pages - and I have to wait until the "print to file" is complete before triggering further action. I'm monitoring FILE_NOTIFY_CHANGE_SIZE
and FILE_NOTIFY_CHANGE_LAST_WRITE
- so I will get periodic notices that the file is still "being printed to".
I'm looking for a safe way to actually detect when the "print" is complete. I thought I might set a timer up in the form, and call Synchronize(StillPrinting)
in the thread. StillPrinting
would stop and restart the timer such that only after some 300 ms of NO folder changes would the timer execute - triggering an OnAfterPrintComplete
event. That just seems dangerous.
Is there anything I can look for to determine if the file save has been completed?
"File has been completed" is subjective - just think about it for 5 minutes and you'll come to the conclusion you're asking for something that rarely makes sense:
No. But there are various approaches to achieve the same:
R
could signal that no more writes will occur. Because you as a consumer only want read access anyway. Of course, the "printer" has to set it.FILE_SHARE_READ
) - this should fail as long as another process accesses the file. You can expect the printer job to not open/close the file multiple times (although that's a possibility, too).