none
Using SystemFileWatcher and threads RRS feed

  • Question

  • I have a console app that will be converted shortly into a Windows Service.  The application calls a function called GetSubDirs() that gets all the subfolders under a parent folder and returns an array of the subfolder names.  It then loops through each subfolder and puts a systemfilewatcher on each one  - looking for any Word or Excel files that have changed and then doing other operations on the detected file.   The for loop code I use to instantiate each SystemFileWatcher object is as follows:

    For Each strDir As String In GetSubDirs("C:\PARENTFOLDER")
                Dim watcher As New FileSystemWatcher()
                watcher.Path = strDir
                watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
                watcher.Filter = "*.*"
                AddHandler watcher.Renamed, AddressOf OnRenamed
                watcher.EnableRaisingEvents = True
            Next 

    This works fine but I am wondering if there is a better, smarter or faster way - should I be using a new thread for each watcher object or is that not crucial?

    Thanks in advance..
    • Edited by kadelglass Tuesday, August 12, 2008 2:57 PM grammtical error
    Tuesday, August 12, 2008 2:52 PM

Answers