C# 您能否使用匿名委托在控制台应用程序中创建多个观察者?
在另一次提问中,我得到了一个关于使用匿名代表的提示。该功能适用于单个观察者,但当我创建三个观察者时,它只保留最后一个观察者。这是因为匿名委托吗?是否有解决方案 我已经添加了代码C# 您能否使用匿名委托在控制台应用程序中创建多个观察者?,c#,c#-4.0,filesystemwatcher,anonymous-methods,C#,C# 4.0,Filesystemwatcher,Anonymous Methods,在另一次提问中,我得到了一个关于使用匿名代表的提示。该功能适用于单个观察者,但当我创建三个观察者时,它只保留最后一个观察者。这是因为匿名委托吗?是否有解决方案 我已经添加了代码 foreach (ConfigurationSectionGroup sectionGroup in sectionGroups) { if (sectionGroup.Name == "FileCheckerConfigGroup") { foreach(ConfigurationSec
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);
}
};
}
}
}
这是因为您使用的是相同的变量
watcher
。尝试在每次迭代中重新创建一个新的观察者:
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);
}
};
}
}
}
您现在要做的是覆盖以前的观察者。。这就是为什么只有最后定义的观察者才能工作 不知道这是否有效:
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);
}
};
}
}
}
我认为问题在于,在lambda中需要foreach循环之外的元素。在循环中创建它的本地副本,一切都应该正常工作:
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);
}
};
}
}
}
为了更好地解释问题所在,请看一看。包含要监视的目录的部分仅在循环中创建,并且对于每个监视者都会有所不同。哦,是的,当然。。您只需要初始化一个新的观察程序,而不是覆盖同一个观察程序。在“FileSystemWatcher=newfilesystemwatcher(第[“inputDirectory”]”部分中更改行“watcher=newfilesystemwatcher(第[“inputDirectory”]”);这是一个有趣的博客。谢谢你给我指出这一点。