2013-02-19 5 views
0

BindingList<Type>에 이진 파일에서 요소를 추가 할 때 이상한 문제가 발생했습니다.한 목록에서 다른 목록으로 요소를 추가 할 때 IOException

BindingList<Type> tmpList = null; 
using (FileStream serializedListStream = File.OpenRead(_kReificableDroidsListPath)) 
{ 
    BinaryFormatter serializer = new BinaryFormatter(); 

    //Temporary list so that ListChanged event is fired 
    //upon adding new types 
    tmpList = serializer.Deserialize(serializedListStream) as BindingList<Type>; 
    foreach (Type droidType in tmpList) 
    { 
     ReificableDroids.Add(droidType); 
    } 
} 

는 Visual Studio 디버거 나에게이 예외를 보여줍니다 :이 코드의 결함이 조각 나는 파일이 폐쇄 될 필요가 있다는 것입니다 이해, 그래서 내 foreach했다 무엇

System.IO.IOException: The process cannot access the file 'C:\Users\Zoneur\documents\visual studio 2012\Projects\Droid TP1\Droid TP2\bin\Debug\reificable_droids_list.bin' because it is being used by another process.

그래서 같은 using 문 밖으로 루프 :

BindingList<Type> tmpList = null; 
using (FileStream serializedListStream = File.OpenRead(_kReificableDroidsListPath)) 
{ 
    BinaryFormatter serializer = new BinaryFormatter(); 

    //Temporary list so that ListChanged event is fired 
    //upon adding new types 
    tmpList = serializer.Deserialize(serializedListStream) as BindingList<Type>; 
} 
foreach (Type droidType in tmpList) 
{ 
    ReificableDroids.Add(droidType); 
} 

예외가 더 이상 발생하지 않고, 그럴 수 없어 문제가 무엇인지 이해하십시오. ReificableDroidsBindingList<Type>의 속성입니다. 또한

, 나는 using 문에 내 foreach 루프를 유지하고 그냥이 같은 콘솔에 유형 이름을 인쇄하는 경우 :

foreach (Type droidType in tmpList) 
{ 
    Console.WriteLine(droidType.GetType().ToString()); 
} 

예외가 하나 발생하지 않습니다.

누군가이 using 문에서이 예외가 발생할 수있는 원인을 알고 있습니까?

+0

코드 논리에서 이전에 '_kReificableDroidsListPath' 파일을 생성하고 있습니까? 문제를 재현 할 수 없습니다. –

+0

파일이 내 디스크에 존재하며 이전에 생성되었습니다. 게다가 파일을 찾을 수 없다면 예외를 처리합니다. – Zoneur

답변

1

이 예외는로드 된 데이터를 반복하는 장소와 관련이 있음을 알 수 없습니다. 여기에는 표시되지 않는 관련 항목이어야합니다.

검사 목록 :

  • 얼마나 많은 시간이 문제가 발생할 했습니까?
  • AddingNew 또는 ListChanged 개의 이벤트를 BindingList으로 처리하고 거기에서 다시 파일에 액세스 할 수 있습니까?
  • 표시되는 메소드에 대한 동시 호출이 여러 개 있습니까?
+0

실제로'ListChanged' 핸들러에서 파일을 액세스하고있었습니다! MVVM 패턴에 맞게 코드를 리팩터링하고이 처리기를 제거하는 것을 잊었습니다. 감사합니다! 좋은 공제 :) – Zoneur

관련 문제