Author |
Topic: monitoring a file for changes (Read 587 times) |
|
David46
New Member
member is offline


Gender: 
Posts: 4
|
 |
monitoring a file for changes
« Thread started on: Dec 30th, 2009, 6:14pm » |
|
I need to monitor a text file which is being periodically re-written by another program, and when the contents change, read the new contents, perform some actions, and return to monitoring it for the next change. I also need to have a timeout on the monitoring loop, and perform another action at timeout before returning to the monitoring loop.
The first possibility I can think of is to loop, trying to open the specific file for update, until I can't, then keep testing until I can, and so assume that it has been changed, and read the contents. This seems a bit clumsy and potentially CPU-time intensive.
There seem to be some Windows (API?) functions that could do this for me - perhaps "FindFirstChangeNotification" and "WaitForSingleObject" but I am not sure how these are to be used from BB4W.
In this particular application, the file I'm interested in is the only object in the folder, and the filename is always the same, so only detecting a "change in a folder" would be just as good, if this is easier.
David
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: monitoring a file for changes
« Reply #1 on: Dec 30th, 2009, 10:29pm » |
|
Quote:There seem to be some Windows (API?) functions that could do this for me - perhaps "FindFirstChangeNotification" and "WaitForSingleObject" but I am not sure how these are to be used from BB4W. |
|
Yes, that is the most elegant solution and those API functions pose no particular difficulties as regards being called from BB4W. The code below works for me.
Richard.
Code: FILE_NOTIFY_CHANGE_LAST_WRITE = &10
ON CLOSE PROCcleanup : QUIT
ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : PROCcleanup : QUIT
folder$ = "C:\Program Files"
SYS "FindFirstChangeNotification", folder$, FALSE, \
\ FILE_NOTIFY_CHANGE_LAST_WRITE TO hNotify%
IF hNotify% = -1 ERROR 100, "FindFirstChangeNotification failed"
timeout% = 100 : REM 0.1 second
REPEAT
SYS "WaitForSingleObject", hNotify%, timeout% TO result%
IF result% = 0 THEN
PRINT "A file was changed"
SYS "FindNextChangeNotification", hNotify%
ENDIF
REM Do something else if required
UNTIL FALSE
END
DEF PROCcleanup
hNotify% += 0 : IF hNotify% SYS "FindCloseChangeNotification", hNotify%
ENDPROC
|
|
Logged
|
|
|
|
|