简体   繁体   English

使数据可用于FileSystemWatcher事件处理程序

[英]Make data available to FileSystemWatcher Event Handler

I have C# 5.0 application that involves using a FileSystemWatcher on a number of different directories. 我有C#5.0应用程序,涉及在多个不同目录上使用FileSystemWatcher

This application contains a List<> of objects. 该应用程序包含对象的List<> 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. 每个对象都描述了受监视的目录以及FileSystemWatcher事件处理程序需要访问的许多其他数据。 These objects are of type CustomDirectorySetting . 这些对象的类型为CustomDirectorySetting

This is a server application, and I expect that the FileSystemWatcher 's event handlers will be called quite often. 这是一个服务器应用程序,我希望FileSystemWatcher的事件处理程序会经常被调用。 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? 如何使FileSystemWatcherCustomDirectorySetting对象快速可用于每个FileSystemWatcher的事件处理程序,我是否应该考虑使用Task Parallel Library实现此实现? I'm a novice at TPL, so I'd appreciate your thoughts on what aspects would be most appropriate. 我是TPL的新手,所以感谢您对哪些方面最合适的想法。

由于FileSystemWatcher似乎可用作基类,因此您可以从中派生自己的类并添加CustomDirectorySetting类型的属性。

Contrary to what nvoigt said, I think creating a type derived from FileSystemWatcher is an overkill. 与nvoigt所说的相反,我认为创建一个从FileSystemWatcher派生的类型是过大的。

A better option would be to have the event handler on your CustomDirectorySetting type, which means it will be able to access the data you need through this . 更好的选择是将事件处理程序设为CustomDirectorySetting类型,这意味着它将能够通过this访问您需要的数据。

class CustomDirectorySetting
{
    public string Directory { get; set; }

    public void OnChanged(object sender, FileSystemEventArgs e)
    {
        // your code here
    }
}

…

List<CustomDirectorySetting> list = …;
foreach (var setting in list)
{
    var fsw = new FileSystemWatcher(setting.Directory);
    fsw.Changed += setting.OnChanged;
    fsw.EnableRaisingEvents = true;
}

Another option is to use a lambda: 另一种选择是使用lambda:

private static void OnChanged(
    CustomDirectorySetting setting, FileSystemEventArgs eventArgs)
{
    // your code here
}

…

foreach (var setting in list)
{
    var fsw = new FileSystemWatcher(setting.Directory);
    CustomDirectorySetting settingCopy = setting;
    fsw.Changed += (sender, eventArgs) => OnChanged(settingCopy, eventArgs);
    fsw.EnableRaisingEvents = true;
}

In this case, it's probably a good idea not to use the loop variable directly in the lambda, because it wouldn't work correctly in older versions of C#. 在这种情况下,最好不要直接在lambda中使用循环变量,因为它在较旧的C#版本中无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM