3

I implemented FileSystemWatcher in a windows forms application. Changed event is working when I open a file (say pdf-1) for the first time. But when I open the same file for the second time within short span of time, changed event is not firing. But it is firing when I open another file (say pdf-2). Again the changed event is firing for the first file (pdf-1) only if I open it after some time (say 1 or 2 hours).

I set the InternalBufferSize to 16KB and the NotifyFilters used are LastAccess, LastWrite, FileName and DirectoryName.

I am unable to find out the issue. Can anyone help me out?

DIF
  • 2,470
  • 6
  • 35
  • 49
  • 1
    I suspect that since the file was recently opened, it is still in the RAM cache. So, when you open it a second time, it doesn't actually involve the filesystem. I could be completely off on this, but see if you can detect explicitly when a file is opened, not just when something changes in the filesystem. That may have some difference. Or, it could be something else entirely that someone else will have to suggest – Russell Uhl Jun 27 '14 at 12:42
  • 1
    Check [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms724290%28v=vs.85%29.aspx) for information about NTFS delaying updates by up to one hour for last access. – HABO Jun 27 '14 at 12:51
  • @RussellUhl Yeah even I guessed the same reason. But I am not sure and I don't know how to clear a file from RAM cache/OS cache – Krishna Teja Jun 27 '14 at 12:52
  • @HABO So, is there a way to avoid delay for last access in c#? – Krishna Teja Jun 27 '14 at 13:03
  • Use a file system that behaves more to your liking (though I don't know of one offhand) or write your own file system. You can't work around it by polling since NTFS isn't updating the file header for multiple accesses within one hour. If the applications accessing the files are all your own then you could modify them to generate notifications, but that doesn't seem likely with PDFs. – HABO Jun 27 '14 at 13:08
  • @HABO Thanks for ur replies. Is it not possible to reduce the delay in NTFS? I mean how to overcome this problem apart from writing own file system? I need to implement it in NTFS only – Krishna Teja Jun 27 '14 at 13:39

1 Answers1

0

I have seen similar results with FileSystemWatcher for RAID controller disks, SCSI disks and regular IDE disks with write cache enabled.

I also experienced a lot of other errors that may have been caused by thread synchronization problems. Here is another description of this problem.

Community
  • 1
  • 1
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164