2011-01-17 2 views
0

다른 Question에서 나는 익명의 대표자를 사용하는 것에 대한 팁을 얻었다. 기능은 단일 관찰자에 대해 작동하지만 3을 만들면 마지막 관찰자 만 유지됩니다. 익명의 대리인 때문에이 문제가 해결 되었습니까?익명의 위임자를 사용하여 콘솔 응용 프로그램에서 여러 명의 관찰자를 만들 수 있습니까?

코드를 추가했습니다.

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); 
       } 
      };        
     } 
    } 
} 

답변

1

문제는 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); 
       } 
      };        
     } 
    } 
} 

this blog from Eric을 살펴 잘못되어 가고 무슨 더 나은 설명은.

+0

이것은 intresting 블로그입니다. 이 하나를 내게 알려 주셔서 감사합니다. – Andy

1

동일한 변수 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); 
       } 
      };        
     } 
    } 
} 
0

당신 .. 지금은 마지막으로 정의 된 감시자가 작동하는 이유 .. 이전 감시자을 덮어이다 할

이 작동하는 경우

모르는 :

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); 
          } 
         };        
        } 
       } 
      } 
+0

감시 할 디렉토리가 포함 된 섹션은 루프에서만 생성되며 모든 감시자마다 다를 수 있습니다. – Andy

+1

oops yes ofcourse .. 동일한 감시자를 덮어 쓰는 대신 새 감시자를 초기화하면됩니다. 라인 변경 "watcher = new FileSystemWatcher (섹션 ["inputDirectory "]);" "FileSystemWatcher watcher = new FileSystemWatcher (section ["inputDirectory "]); " – BvdVen

관련 문제