Parallel.Foreach 루프에서 별도의 csv 컬렉션을 작성하려고합니다. 이 프로세스를 실행할 때 다음 오류가 발생합니다.StreamWriter를 사용하여 Parallel.Foreach의 다른 파일에 쓰기
다른 프로세스에서 사용 중이기 때문에 프로세스가 'C : \ temp \ OCRProcess \ 2011 \ 49 \ OCRProcess-2011-49.csv'파일에 액세스 할 수 없습니다. .
모든 작업자 프로세스를 살펴보면 다른 파일이 열려 있으며 둘 다 동일한 파일 위치를 사용하지 않습니다. 내가 알 수 있듯이, 각 스레드 (작업자 프로세스)는 충돌하지 않습니다.
public void GenerateCSVFile(List<CSVFile> csvFiles)
{
Parallel.ForEach(csvFiles, (csvfile, state, index) =>
{
var fileLocation = _storageLocation + csvfile.Type + s + csvfile.Year + s + csvfile.Day + s + csvfile.Type + "-" +
csvfile.Year + "-" + csvfile.Day + ".csv";
if (!File.Exists(fileLocation))
{
File.Create(fileLocation);
}
using (var streamWriter = new StreamWriter(fileLocation))
{
foreach (var csvRecord in csvfile.CSVRecords)
{
streamWriter.WriteLine(csvRecord.Object_Id + "," + csvRecord.DocumentName + "," + csvRecord.BCI_DCN + "," + csvRecord.CreationDate);
}
}
});
}
다음은 방금 넣은 CSVFile 및 CSVRecord 클래스입니다.
public sealed class CSVRecord
{
public String BCI_DCN { get; set; }
public String Object_Id { get; set; }
public String DocumentName { get; set; }
public String CreationDate { get; set; }
}
public sealed class CSVFile
{
public List<CSVRecord> CSVRecords { get; set; }
public String Year { get; set; }
public String Type { get; set; }
public String Day { get; set; }
public CSVFile()
{
CSVRecords = new List<CSVRecord>();
}
}
목록에 중복 된 csv 파일이있을 수 있습니까? csvFiles? –
Jason
목록 csvFiles를 보았으며 중복 된 부분이 없습니다. –
munchrall
동일한 파일에서 항상 문제가 발생합니까? – Jason