Announcement

Collapse
No announcement yet.

Watch directory using another thread

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Watch directory using another thread

    Would it be better to use a timer to check for a directory change?
    This uses 2 globals and not sure how to get around it to signal thread to end.
    Also, this code is a bit different than some posted code so improvements are welcome.

    Code:
    #DIM ALL
    #INCLUDE "win32api.inc"
    GLOBAL gWaitInMilliseconds AS LONG
    GLOBAL gDirectoryToWatch AS ASCIIZ * 64
    FUNCTION PBMAIN AS LONG
      LOCAL hThread???, var&, h&, x&, lResult&
      gWaitInMilliseconds = 3000
      gDirectoryToWatch  = "\keep\email"
      MKDIR gDirectoryToWatch   'create directory
      ERRCLEAR
      THREAD CREATE WatchDirectory(var&) TO hThread???
      SLEEP 50
      THREAD CLOSE hThread TO lResult
     
      h = FREEFILE
      OPEN gDirectoryToWatch  + "\TEST" FOR APPEND AS #h
      FOR x = 1 TO 5                                         'write something every second
        PRINT  #h, "MORE"
        IF ERR THEN ? STR$(ERR)
        SLEEP 1000
      NEXT
      CLOSE #h
      gWaitInMilliseconds = 0   'signal thread to end
      SLEEP 4000  'give thread time to end
    END FUNCTION
    FUNCTION WatchDirectory (BYVAL x AS LONG) AS LONG
      LOCAL lDir AS DWORD, found AS DWORD
      DO
        lDir = FindFirstChangeNotification(gDirectoryToWatch, 0, %FILE_NOTIFY_CHANGE_SIZE)
        Found = WaitForSingleObject(lDir, gWaitInMilliseconds)
        IF found = 0 THEN
          'directory changed
          BEEP
        END IF
        FindCloseChangeNotification(lDir)
      LOOP UNTIL gWaitInMilliseconds=0
      ? "Thread done"
    END FUNCTION
    Last edited by Mike Doty; 22 Feb 2008, 03:26 AM.
    How long is an idea? Write it down.

  • #2
    Win32(SDK): Internet Cookie Monitor April 25, 2001

    No GLOBALs, no timed looping.

    (Will work with any directory, not just the Cookie directory).
    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Well just a few..
      Code:
       ' GLOBAL datanames
      GLOBAL CookiePath AS ASCIIZ * %MAX_PATH
      GLOBAL nChangeEntries AS LONG      ' number of valid entries in the CookieChange array
      GLOBAL CookieChange() AS STRING
      GLOBAL hWndMain       AS LONG      ' because we need to reference it from a thread function
      GLOBAL hDlgAbout      AS LONG      ' when true, the modeless 'About' dialog box is running
      Not that there's anything wrong with that!
      Rgds, Dave

      Comment


      • #4
        But, but... that was so long ago, when I was young and foolish!
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          BUSTED


          Sorry couldn't help myself

          5 globals can be forgiven (amongst the many out there that I have used)

          Anyways had to razz a bit :tongue: so hopefully no harm no foul

          anyways for Mike Doty's question, I can not say for sure, but if you can test on a machine you consider "Extremely slow" and do not notice much of an impact, then I say run with it
          Engineer's Motto: If it aint broke take it apart and fix it

          "If at 1st you don't succeed... call it version 1.0"

          "Half of Programming is coding"....."The other 90% is DEBUGGING"

          "Document my code????" .... "WHYYY??? do you think they call it CODE? "

          Comment

          Working...
          X