BBC BASIC for Windows
« monitoring a file for changes »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 10:15pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: monitoring a file for changes  (Read 587 times)
David46
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 4
xx 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx 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 
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls