0
我有一个并行foreach在完成执行之前关闭连接的问题。当我有一个普通的foreach循环runnung它很慢,但它会返回一切。一旦我改变为并行foreach,它现在返回大约95%的数据并终止。并行Foreach竞争条件
下面是我使用的代码:
var USPostalCodes = repository.GetUSPostalCodes();
var CAPostalCodes = repository.GetCAPostalCodes();
Parallel.ForEach(spreadsheetinfo, location =>
{
LocationData Locationdata = new LocationData()
{
id = location.Id,
Market = repository.GetMarketsForPostalCode(location.PostalCode, uploadedFile, USPostalCodes, CAPostalCodes),
};
locationlist.Add(Locationdata);
});
I added the following code to check and see what was going on, and that fixed it so that it is returning all rows so i know that a race condition exists but what i can't figure out is why and how ot fix it. any suggestions would be greatly appreciated
Console.WriteLine("Processing {0} on thread {1}", Locationdata,
Thread.CurrentThread.ManagedThreadId);