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 t开发者_StackOverflow中文版he 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);
}
};
}
}
}
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.
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);
}
};
}
}
}
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);
}
};
}
}
}
精彩评论