0

In another Question I asked, I got a tip on using an anonymous delegate. The functionality works for a single watcher but when I create three it only keeps the last one. Is this because of the anonymous delegate and is there a solution to this?

I have added the code.

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach(ConfigurationSection configurationSection in sectionGroup.Sections)
        {
            //FileChecker filecheck = new FileChecker();
            //filecheck.ProccessFolders(configurationSection);
            //FileChecker filecheck = new FileChecker();
            var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
            watcher = new FileSystemWatcher(section["inputDirectory"]);
            watcher.EnableRaisingEvents = true;
            watcher.Created += (sender, e) =>
            {
                using (var filecheck = new FileChecker())
                {
                    filecheck.ProccessFolders(configurationSection);
                }
            };                               
        }
    }
}
Community
  • 1
  • 1
Andy
  • 2,248
  • 7
  • 34
  • 57

3 Answers3

1

That's because you are using the same variable watcher. Try recreating a new watcher on each iteration:

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
        {
            var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
            var watcher = new FileSystemWatcher(section["inputDirectory"]);
            watcher.EnableRaisingEvents = true;
            watcher.Created += (sender, e) =>
            {
                using (var filecheck = new FileChecker())
                {
                    filecheck.ProccessFolders(configurationSection);
                }
            };                               
        }
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

I think the problem is that you need within your lambda the element out of the foreach loop. Create a local copy of it within the loop and everything should work fine:

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach(ConfigurationSection configurationSection in sectionGroup.Sections)
        {
            //FileChecker filecheck = new FileChecker();
            //filecheck.ProccessFolders(configurationSection);
            //FileChecker filecheck = new FileChecker();
            var localConfigurationSectionCopy = configurationSection;
            var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
            watcher = new FileSystemWatcher(section["inputDirectory"]);
            watcher.EnableRaisingEvents = true;
            watcher.Created += (sender, e) =>
            {
                using (var filecheck = new FileChecker())
                {
                    filecheck.ProccessFolders(localConfigurationSectionCopy);
                }
            };                               
        }
    }
}

For a better explanation whats going wrong take a look at this blog from Eric.

Oliver
  • 43,366
  • 8
  • 94
  • 151
0

What you do now is overwrite your previous watcher.. that's the reason only the last defined watcher works..

don't know if this works:

watcher = new FileSystemWatcher(section["inputDirectory"]);
 foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
            {
                if (sectionGroup.Name == "FileCheckerConfigGroup")
                {
                    foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
                    {
                        //FileChecker filecheck = new FileChecker();
                        //filecheck.ProccessFolders(configurationSection);
                        //FileChecker filecheck = new FileChecker();
                        var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
                        watcher.EnableRaisingEvents = true;
                        watcher.Created += (sender, e) =>
                        {
                            using (var filecheck = new FileChecker())
                            {
                                filecheck.ProccessFolders(configurationSection);
                            }
                        };                               
                    }
                }
            }
BvdVen
  • 2,921
  • 23
  • 33
  • the section containing the directory to watch is only created in the loop and will be diffrent for every watcher. – Andy Jan 17 '11 at 10:46
  • 1
    oops yes ofcourse.. you you just need to initialize a new watcher instead of overwriting the same watcher. change your line "watcher = new FileSystemWatcher(section["inputDirectory"]);" in "FileSystemWatcher watcher = new FileSystemWatcher(section["inputDirectory"]); " – BvdVen Jan 17 '11 at 10:54