FileSystemWatcher

Is there a way to use on FileSystemWatcher object to watch both a certain type of file and all folders or do you have to use two FileSystemWatcher objects to do that

Also, if two are necessary, how would you go about having both of those work on different threads (I'm pretty new to threading)

Many Thanx!  :) 


Answer this question

FileSystemWatcher

  • Sauvere

    hhhmmm...ok, so two it is...

    well, i was just asking about using two different threads, because i'm kinda clueless  :p 

    I was just wondering if having two FileSystemWatchers is going to be efficient or if there's a better way

    thanx for your help  :) 

  • vincent.wang

    You could simply monitor for changes without the pattern filter and do your own filtering on the notifications.  I'm not sure what will end up being more efficient, but I'll go with the operating system being able to handle 2 file watchers ;).
  • Dmitry Lysenko

    1) AFAIK you cannot combine both file type filtering (.Filter = "*.ext") and directory watching (.NotifyFilter |= NotifyFilters.DirectoryName).  You'll have to use two of them.

    2) I'm not sure I understand your question   Obviously this monitoring is handled by the operating system and I'm not exactly sure how it is handled internally.  If you're asking how to monitor *from* different threads, that's a bit closer to home and I could help you out their.  Let me know.

  • mjepsen

    FWIW, I think that when you use the FileSystemWatcher component, you're handing over control of the threads to the operating system. THe only issue you can control with regards to threading is the SynchronizingObject property, which allows you to specify a particular Windows Forms component (anything that inherits from Control) that basically tells the events raised by the FileSystemWatcher to run on the same thread as the owner of that component. This avoids thread switching issues (which can cause a WinForms app to roll over at runtime). If your event procedures for the FileSystemWatcher need to modify properties of a form (perhaps writing to the form), and you didn't drag the FileSystemWatcher component from the toolbox (in which case the designer sets the SynchronizingObject property for you, to the current form), you'll need to set this property in code. (Check the hidden region when you drag a FileSystemWatcher instance to the form--the designer adds:

    Me.FileSystemWatcher1.SynchronizingObject = Me

    to the code. If you create the instance in code, and don't set this property, and need to write to a WinForm from an event handler, you could have trouble unless you switch to the form's thread before doing so. That's what the SynchronizingObject property does.

    This is the only issue I can think of where you can control threading behavior with this class, however! -- Ken

  • Johodefo

    rock on, thanx  :) 
  • FileSystemWatcher