I have a NT service which scans a particular folder every 15 seconds for any files in that folder. The files in this folder are being moved from another folder by another service. Here's the sequence of events that happens:
1. Every 15 seconds, a function gets called which :
- Makes a call to findfirst().
- For the file found (excluding subdirectories) a row is inserted into the database.
- The file is moved to a different folder.
- An email is sent out.
- Call findnext to work on the next file.
Randomly, the function works on the same file twice. What seems to be happening is that findfirst() or findnext() finds the file in the folder while it is still being moved into the folder or being moved out of the folder. Since windows has still not completed the move, findfirst()/findnext() combo finds the file again and processes it again. I use the MoveFileEx function to move the file with the MOVEFILE_WRITE_THROUGH flag which should guarantee that the function does not return till the file is moved. I have also tried to call CreateFile() to get an exclusive lock on the file before starting any processing on the file.
Somehow, randomly, I still get a file which is processed twice. Any ideas/suggestions?
Thanks & Cheers