I have a FileSystemWatcher monitoring a directory that receives TIF files from a scanning device.
It's necessary to check if the scanning process is done and then handle the fully scanned file, otherwise, my program will handle an incomplete file.
I have something like:
private void OnFileCreated(...)
{
while(IsFileLocked(path))
Thread.Sleep(time);
// OK to read
}
This is what is happening:
- Scanner creates the file
- FileSystemWatcher detects the file, but its in use
- Scanner reads the first page to the file
- Scanner releases the file
- My code leaves the while(IsFileLocked(path))
- My code reads the incomplete file (problem)
- Scanner adds more pages to the file
Let's say the scanner is scanning 100 pages, then, when "OK to read", the file will be incomplete (99 pages left).
So, its necessary to know if the file is complete or not. Maybe waiting some time to see if the file is modified, but this time span can be up to hours, because the scanner can get idle scanning the same TIF. Other solution would be checking some flag in the TIF file that indicates that the file is not complete(I've looked for this but don't found anything).
Edit: before asking here, I had read the TIFF format documentation. I found the attribute PageNumber interesting, but do not know if it would solve my problem