I'm currently trying to get a FileSystemWatcher to work, as outlined in this question. During my research, I found a lot of answers and comments on this site describing the unreliability of this class. Instead, "polling" is mentioned in some places as a method of watching for changes, for example in this (very old) answer. My question is what that means exactly.
Asked
Active
Viewed 493 times
3
-
1There is a `Polling (Computer Science)` page on Wikipedia. – Xiang Wei Huang Jul 12 '22 at 09:59
-
1It means you fetch file metadata periodically and calculate diffs to the previous results. – Fildor Jul 12 '22 at 10:00
-
It just means that code actively goes and enumerates the files in a directory and compares the list with the last time they were enumerated to see if anything has changed, rather than relying on a callback from the operating system to tell you that something has changed. – Matthew Watson Jul 12 '22 at 10:00
-
BTW: It has been quite some time since I have been using the FSW, but if you know its quirks and how to handle them I did not find it unreliable at all. But I encourage you to neither believe me nor anyone else. Better go and try for yourself. – Fildor Jul 12 '22 at 10:01
1 Answers
7
Polling means here that you regularly read for instance the last write-time of the files you want to watch and test if there is a difference. you could even read the file contents and compare it with a previous verison. Polling just means that you actively do the comparison instead of being notified.
Polling is best avoided for its cost. But if needed then it is needed.
https://en.wikipedia.org/wiki/Polling_(computer_science)
Regarding FileSystemwatcher. It is not perfect but in my experience correct enough in the majority of the cases . I assume that all watchers in developer tools use that mechanism and it is for instance good enough there. I suggest to try it first for your purpose before polling.

citykid
- 9,916
- 10
- 55
- 91