1

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

Davi Fiamenghi
  • 1,237
  • 12
  • 28
  • 2
    what does the `FileInUse` function do? – vlad Nov 16 '11 at 22:21
  • 1
    Can you query the scanner to find out when it is done scanning? – UnhandledExcepSean Nov 16 '11 at 22:53
  • It verifies if any program is reading a file, I put this way to keep it simple, but it does something similar to: http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use A better name for this method would be IsFileLocked – Davi Fiamenghi Nov 17 '11 at 01:35
  • SpectralGhost - sorry, I can't... I don't even know which scanner that created the file. I'm looking for a solution in the TIF format itself, I was wondering if some TIF file header can provide me this information. Or even better, a file attribute that devices in general, define to indicate that a file isn't complete – Davi Fiamenghi Nov 17 '11 at 01:38
  • 1
    You are not going to find a metadata item in the TIFF file that says "the scanner isn't done yet". You'll need a better scanner or a heuristic or a better scanner interface, like WIA. – Hans Passant Nov 17 '11 at 02:04
  • I'm convinced that there is no way of doing that with the TIFF format, as Hans said, but since TIFF is a format for scanning, it must be happened before and must be someway to deal with it. I'm just waiting a response of a person that have good scanning knowledge suggesting any alternative path, if it doesn't exists, I will see that the problem is not my code, but the scanner. Thanks for all that helped until now – Davi Fiamenghi Nov 17 '11 at 11:57

3 Answers3

1

Have you considered the Image.FromFile method?

If this function can open it and read a valid tif file then you are okay.

The problem with headers is that they are usually at the front of a file and written first and so the rest of the file is not yet completely written.

You could use an exclusive lock on the file to see if the scanner has finished writing the file, but that depends on the behaviour of the scanner software i.e. does it keep the file locked when writing or does it keep opening/writeblock/close/repeat.

Shaun Wilde
  • 8,228
  • 4
  • 36
  • 56
  • I will try it when possible, thanks. But, Tiff files will be read successfully even with just one page, correct? – Davi Fiamenghi Nov 17 '11 at 02:35
  • It all depends on the format i.e. for a file to be read correctly it may have to be all there. I am not 100% sure if .NET handles multipage tiffs - which is what you seem to be hinting at. Hence the exclusive file locking approach could be an alternative; just suggstions after all. – Shaun Wilde Nov 17 '11 at 02:37
  • Image.FromFile will be inconclusive to check if it's complete, since I can open it whit only one page. I already tryed exclusive file lock, look at my question. – Davi Fiamenghi Nov 17 '11 at 11:51
  • So the scanner does not lock the file all the time until complete? It opens and closes it? If so why not wait 30seconds or so and if the file has not changed size in that time then start processing it. I suspect the scanner will open and close the file and keep adding pages - updating the header in the process with the number of pages so it is always correct. What scanner software are you using? - can you change it's behaviour e.g. so it builds file in location X and then copy to Y when job complete – Shaun Wilde Nov 17 '11 at 21:14
0

As Hans Passant answered on the comment, the only solution I found usefull is:

You are not going to find a metadata item in the TIFF file that says "the scanner isn't done yet". You'll need a better scanner or a heuristic or a better scanner interface, like WIA.

Davi Fiamenghi
  • 1,237
  • 12
  • 28
0

you can test the file size if it's changing or not:

FileInfo fi = new FileInfo(f);
long size = fi.Length;
Application.DoEvents();
System.Threading.Thread.Sleep(5000);
Application.DoEvents();
FileInfo nfi = new FileInfo(f);
long nsize = nfi.Length;
if (size == nsize)
{
    //Do function
}
AbeerAhdab
  • 66
  • 1
  • 8