Search code examples
c#.nettask-parallel-libraryfilesystemwatcher

Make data available to FileSystemWatcher Event Handler


I have C# 5.0 application that involves using a FileSystemWatcher on a number of different directories.

This application contains a List<> of objects. Each object describes the directory being monitored, as well as a lot of other data that needs to be accessed by the FileSystemWatcher event handler. These objects are of type CustomDirectorySetting.

This is a server application, and I expect that the FileSystemWatcher's event handlers will be called quite often. I must ensure that they can keep up with their work and not fall behind responding to file change events. It may be worth adding that the directories that are being monitored are local to the machine running the application.

How can I make the FileSystemWatcher's CustomDirectorySetting object available quickly to the event handler of each FileSystemWatcher, and should I be looking at using the Task Parallel Library for this implementation? I'm a novice at TPL, so I'd appreciate your thoughts on what aspects would be most appropriate.


Solution

  • As the FileSystemWatcher seems to be usable as a base class, you could derive your own class from it and add a property of type CustomDirectorySetting.