How do I monitor rtf file to check if it is updated for a while (lets say 15 min). If not updating then let the main thread know that file is not updated. I am thinking of using WaitforSingleObject function to wait for any changes in last 15 minute. how can I implement this funcationality?
-
2what OS is this used on? http://stackoverflow.com/questions/1938939/get-file-last-modify-time-and-compare http://stackoverflow.com/questions/1938939/get-file-last-modify-time-and-compare - or just cache the contents (or a hash of it) with a timestamp and compare it on timeout – x29a Aug 30 '13 at 07:05
-
Because WaitForSingleObject from winapi, I'll just leave this here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261(v=vs.85).aspx – user1837009 Aug 30 '13 at 07:14
-
@scones This is a windows question, and anyway you used the wrong source for linux docs. You should refer to kernel.org. – David Heffernan Aug 30 '13 at 07:15
4 Answers
I believe what are looking for is file change notifications such as FindFirstChangeNotification
, FindNextChangeNotification
, and ReadDirectoryChangesW
you monitor a file or directory for changes, rename, write, and so on.
-
The issue with FindFirstChangeNotification/FidNextChangeNotification is that, it notifies you of changes on any/all file(s) in given directory. But in my case I'm only interested in a particular file not all. is there any way to set the filter for a file? – user2731777 Sep 01 '13 at 11:20
-
@user2731777 I'm not aware of such function. You could use `ReadDirectoryChangesW` and filter the notification based of name, using `wcscmp`, form `FILE_NOTIFY_INFORMATION.FileName`. – Sep 01 '13 at 17:10
Presumably your platform is Windows since you mention WaitForSingleObject. In which case the function you are looking for is ReadDirectoryChangesW. This will allow you to be notified as soon as changes are made, without you performing any polling.
Jim Beveridge has an excellent pair of articles that go into some depth:

- 601,492
- 42
- 1,072
- 1,490
You can stat()
the file, check its modification date and act appropriately.
You can also periodically compute a checksum of the file and compare it to the previous one.
For RTF files you can also take the size of the file and compare it to the previous size; if it's been modified it's very likely the size will be different.
All those methods will probably introduce more overhead than the system calls mentioned by others.

- 21,561
- 9
- 74
- 114
-
Whilst you can do this, it involves polling which is inelegant and inefficient – David Heffernan Aug 30 '13 at 07:22
-
I'm posting alternatives to what you have wrote. They have disadvantages, but are valid and can be used. – Dariusz Aug 30 '13 at 07:53
-
But _why_ would one want to use an inelegant, inefficient method? – Carey Gregory Aug 30 '13 at 15:31
-
Carey Gregory: Because the OS API also has - let's say it politely - very serious and inelegant flaws in this particular case. Both `FindFirstChangeNotification` and `ReadDirectoryChangesW` do not listen for changes in symlinked files/sub directories (only to the symlinks itself). `ReadDirectoryChangesW` locks the monitored directory, which often is a big no-no. `FindFirstChangeNotification` doesn't tell you anything about the operation which caused the notification. And `WaitForMultipleObjects` is limited to 64 objects, which makes things complicated (additional threads necessary) – user2328447 Aug 26 '18 at 08:48
In my opinion, you can achieve this in two ways. You can write a file filter driver that can monitor write operation on the file. However this is little bit stretching.
Another way is simple one. In your main thread, create a hash of your RTF file and cache it. Create an event in non-signaled state, create a callback function, create a worker thread. Wait in the worker thread on event for 15 min. After timout, again generate hash of your file and compare it with cached hash. If mismatch, notify your main thread through callback function.

- 87
- 1
- 6